2013-06-22 115 views
67

這是一個非常簡單的問題,但我在Google上沒有找到任何東西。我在Node.js的創建服務器函數中尋找有關請求參數的文檔,但我一直無法找到任何東西。Node.js請求對象文檔?

http.createServer(function(request, response){ 
    console.log(JSON.stringify(request)); 
}); 

調試與JSON.stringify()給我一個錯誤,該對象是圓形的,並且在程序停止。我見過像request.url,或request.body不同的事情,但有記錄所有的請求的功能和參數的頁面?它似乎應該很容易找到,我似乎無法找到它。

我又試了一下,原來只是做console.log(request)寫出所有的請求的數據。這只是作爲一個參考:

ondata: [Function], 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular] } }, 
    connection: 
    { _connecting: false, 
    _handle: 
     { fd: null, 
     writeQueueSize: 0, 
     owner: [Circular], 
     onread: [Function: onread], 
     reading: true }, 
    _readableState: 
     { highWaterMark: 16384, 
     buffer: [], 
     length: 0, 
     pipes: null, 
     pipesCount: 0, 
     flowing: false, 
     ended: false, 
     endEmitted: false, 
     reading: true, 
     calledRead: true, 
     sync: false, 
     needReadable: true, 
     emittedReadable: false, 
     readableListening: false, 
     objectMode: false, 
     defaultEncoding: 'utf8', 
     ranOut: false, 
     awaitDrain: 0, 
     readingMore: false, 
     decoder: null, 
     encoding: null }, 
    readable: true, 
    domain: null, 
    _events: 
     { end: [Object], 
     finish: [Function: onSocketFinish], 
     _socketEnd: [Function: onSocketEnd], 
     drain: [Function: ondrain], 
     timeout: [Function], 
     error: [Function], 
     close: [Object] }, 
    _maxListeners: 10, 
    _writableState: 
     { highWaterMark: 16384, 
     objectMode: false, 
     needDrain: false, 
     ending: false, 
     ended: false, 
     finished: false, 
     decodeStrings: false, 
     defaultEncoding: 'utf8', 
     length: 0, 
     writing: false, 
     sync: true, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [] }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    errorEmitted: false, 
    bytesRead: 357, 
    _bytesDispatched: 0, 
    _pendingData: null, 
    _pendingEncoding: '', 
    server: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 1, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 120000, 
     _connectionKey: '4:0.0.0.0:1337' }, 
    _idleTimeout: 120000, 
    _idleNext: 
     { _idleNext: [Circular], 
     _idlePrev: [Circular], 
     msecs: 120000, 
     ontimeout: [Function: listOnTimeout] }, 
    _idlePrev: 
     { _idleNext: [Circular], 
     _idlePrev: [Circular], 
     msecs: 120000, 
     ontimeout: [Function: listOnTimeout] }, 
    _idleStart: 1371952289619, 
    parser: 
     { _headers: [], 
     _url: '', 
     onHeaders: [Function: parserOnHeaders], 
     onHeadersComplete: [Function: parserOnHeadersComplete], 
     onBody: [Function: parserOnBody], 
     onMessageComplete: [Function: parserOnMessageComplete], 
     socket: [Circular], 
     incoming: [Circular], 
     maxHeaderPairs: 2000, 
     onIncoming: [Function] }, 
    ondata: [Function], 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular] } }, 
    httpVersion: '1.1', 
    complete: false, 
    headers: 
    { host: 'localhost:1337', 
    connection: 'keep-alive', 
    'cache-control': 'max-age=0', 
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/27.0.1453.116 Safari/537.36', 
    'accept-encoding': 'gzip,deflate,sdch', 
    'accept-language': 'en-US,en;q=0.8,ja;q=0.6' }, 
    trailers: {}, 
    _pendings: [], 
    _pendingIndex: 0, 
    url: '/', 
    method: 'GET', 
    statusCode: null, 
    client: 
    { _connecting: false, 
    _handle: 
     { fd: null, 
     writeQueueSize: 0, 
     owner: [Circular], 
     onread: [Function: onread], 
     reading: true }, 
    _readableState: 
     { highWaterMark: 16384, 
     buffer: [], 
     length: 0, 
     pipes: null, 
     pipesCount: 0, 
     flowing: false, 
     ended: false, 
     endEmitted: false, 
     reading: true, 
     calledRead: true, 
     sync: false, 
     needReadable: true, 
     emittedReadable: false, 
     readableListening: false, 
     objectMode: false, 
     defaultEncoding: 'utf8', 
     ranOut: false, 
     awaitDrain: 0, 
     readingMore: false, 
     decoder: null, 
     encoding: null }, 
    readable: true, 
    domain: null, 
    _events: 
     { end: [Object], 
     finish: [Function: onSocketFinish], 
     _socketEnd: [Function: onSocketEnd], 
     drain: [Function: ondrain], 
     timeout: [Function], 
     error: [Function], 
     close: [Object] }, 
    _maxListeners: 10, 
    _writableState: 
     { highWaterMark: 16384, 
     objectMode: false, 
     needDrain: false, 
     ending: false, 
     ended: false, 
     finished: false, 
     decodeStrings: false, 
     defaultEncoding: 'utf8', 
     length: 0, 
     writing: false, 
     sync: true, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [] }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    errorEmitted: false, 
    bytesRead: 357, 
    _bytesDispatched: 0, 
    _pendingData: null, 
    _pendingEncoding: '', 
    server: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 1, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 120000, 
     _connectionKey: '4:0.0.0.0:1337' }, 
    _idleTimeout: 120000, 
    _idleNext: 
     { _idleNext: [Circular], 
     _idlePrev: [Circular], 
     msecs: 120000, 
     ontimeout: [Function: listOnTimeout] }, 
    _idlePrev: 
     { _idleNext: [Circular], 
     _idlePrev: [Circular], 
     msecs: 120000, 
     ontimeout: [Function: listOnTimeout] }, 
    _idleStart: 1371952289619, 
    parser: 
     { _headers: [], 
     _url: '', 
     onHeaders: [Function: parserOnHeaders], 
     onHeadersComplete: [Function: parserOnHeadersComplete], 
     onBody: [Function: parserOnBody], 
     onMessageComplete: [Function: parserOnMessageComplete], 
     socket: [Circular], 
     incoming: [Circular], 
     maxHeaderPairs: 2000, 
     onIncoming: [Function] }, 
    ondata: [Function], 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular] } }, 
    _consuming: false, 
    _dumped: false, 
    httpVersionMajor: 1, 
    httpVersionMinor: 1, 
    upgrade: false } 
+48

你有我的同情。大多數現代語言,圖書館和框架都有令人震驚的參考文獻。如果你訪問下面提到的節點文檔,你會發現它實際上並不是一個參考;這更多的是討論。例如,createServer()doc表示它將參數「一個自動添加到'request'事件的函數」並返回「一個Web服務器對象」。沒有暗示這些東西是什麼「類型」,並且肯定沒有關於這些東西的參考文檔的鏈接。他們是什麼? F減去文檔。 – jarmod

回答

22

我不知道這是否是一個正確的答覆,因爲它似乎直截了當給我,但你看看 http://nodejs.org/api/http.html#http_http_incomingmessage

請求是上述目的的實例。順便說一句,如果你真的想看到物體內部有什麼用「蠻力」,你不應該分析它,因爲它不會是一個JSON字符串在所有的,而是做這樣的事情:

for (i in request) { 
    //Your code here 
} 

希望能幫助到你。

+0

糟糕,我的意思是json.stringify,而不是json.parse。回過頭去嘗試一下你提到的for(i in request)方法,出於某種原因,只是做了console.log(request)就結束了工作。我期望它只是說,[Object object]。也許它有一個特定的控制檯toString函數?謝謝回覆。 – Seiche

+4

更好的是,要查看任何對象內部的內容,請使用util.inspect()。 Util是一個核心模塊;你可以簡單地使用'var util = require('util');'和util.inspect(obj)'。 'JSON.stringify()'的一個基本問題是,如果有循環引用,它會投訴。 –

+1

'_readableState','readable','domain','_events','_maxListeners','socket','connection','httpVersion','complete','headers','trailers','_pendings','' _displayIndex','url','method','statusCode','client','_consuming','_dumped','httpVersionMajor','httpVersionMinor','upgrade','setTimeout','read','_read' ,'destroy','_addHeaderLine','_dump','push','unshift','setEncoding','pipe','unpipe','on','addListener','resume','pause', 'setMaxListeners','emit','once','removeListener','removeAllListeners','listeners' – CaffeineAddiction

6

我同意德利奧,請求對象的類型是IncomingMessage對象,這是由http.ClientRequest創建的。

8

我想補充的,如Node.js的相對新手,我覺得很混亂,每個框架都有自己實施「請求」的對象。

例如,

快遞:http://expressjs.com/api.html#req.param

哈皮:http://spumko.github.io/resource/api/#request-object(只是要注意的是原始請求對象仍處於request.raw.req可用)

一些庫假定存在Express''req.param'方法,並且在非Express項目中使用時會失敗。

+0

非常感謝非常適合分享快速API選項。 –

+0

Spumko鏈接已損壞。 –

+0

解析文檔比大多數更完整:https://parse.com/docs/js/symbols/http.IncomingMessage.html –