2012-09-27 72 views

回答

1

爲了更好地理解內在的的NodeJS模塊之一,你應該先閱讀其文檔。然後,如果需要進一步理解,您將需要探索定義它的源代碼。 (大多數內在模塊本身都是用JavaScript編寫的)。

例如,ServerResponse對象(通常作爲「res」傳入HTTP請求處理程序)在http.js的第867行中定義,可以在nodejs源代碼https://github.com/joyent/node/blob/master/lib/http.js

通過探索源代碼,您將能夠看到對象支持的方法和屬性。

然後,要完全理解一個對象是如何工作的並且發現其方法和屬性的全部,則需要遵循其繼承樹。

ServerResponse從

OutgoingMessage(在相同的文件中定義的),其本身從

流(在stream.js定義)繼承繼承其進一步繼承

events.EventEmitter,當然是

對象

所以,方法和ServerResponse的特性的完整列表是:

ServerResponse 

    statusCode    property 
    _implicitHeader()  method 
    assignSocket()   method 
    detachSocket()   method 
    writeContinue()  method 
    writeHead()   method 
    writeHeader()   method 

    _buffer()    (from OutputMessage) method 
    _finish()    (from OutputMessage) method 
    _flush()    (from OutputMessage) method 
    _renderHeaders()  (from OutputMessage) method 
    _send()    (from OutputMessage) method 
    _storeHeader()   (from OutputMessage) method 
    _writeRaw()   (from OutputMessage) method 
    addTrailers()   (from OutputMessage) method 
    destroy()    (from OutputMessage) method 
    end     (from OutputMessage) method 
    getHeader()   (from OutputMessage) method 
    removeHeader()   (from OutputMessage) method 
    setHeader()   (from OutputMessage) method 
    write()    (from OutputMessage) method 

    pipe()     (from Stream) method 

    addListener()   (from EventEmitter) method 
    emit()     (from EventEmitter) method 
    listeners()   (from EventEmitter) method 
    on()     (from EventEmitter) method 
    once()     (from EventEmitter) method 
    removeAllListeners() (from EventEmitter) method 
    removeListener()  (from EventEmitter) method 
    setMaxListeners()  (from EventEmitter) method 

    arguments    (from Object) property 
    caller     (from Object) property 
    length     (from Object) property 
    name     (from Object) property 
    prototype    (from Object) property 
    super_     (from Object) property 

    __defineGetter__()  (from Object) method 
    __defineSetter__()  (from Object) method 
    __lookupGetter__()  (from Object) method 
    __lookupSetter__()  (from Object) method 
    constructor()   (from Object) method 
    hasOwnProperty()  (from Object) method 
    isPrototypeOf()  (from Object) method 
    propertyIsEnumerable() (from Object) method 
    toLocaleString()  (from Object) method 
    toString()    (from Object) method 
    valueOf()    (from Object) method 
    apply()    (from Object) method 
    bind()     (from Object) method 
    call()     (from Object) method 

通過跟隨對象的整個繼承樹,你建一個深入瞭解nodejs是如何工作的,並瞭解它是一個了不起的工具。

+0

謝謝,羅布。我可以同樣獲得請求支持和方法嗎? – vinod

+0

如何獲取整個html頁面?是否有可能通過任何「res」屬性? – VVB

2

最簡單的方法來找出其中的JavaScript功能,性能,...一個對象是將對象序列化爲一個字符串並將該字符串輸出到控制檯。

基本上這是一個一行,如:

console.log(JSON.stringify(req)); 

這說明你的一切req對象了。