2014-10-20 143 views
0

如何記錄Node.js請求?如何記錄Node.js請求

var http = require('http'); 
http.createServer(function handler(req, res) { 
    console.log(req); 
}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:1337/'); 

例如,使用console.log(req);的時候,我得到長req對象見下文,但用於記錄它應該像短得多。

GET/HTTP/1.1 
Host: 127.0.0.1:1337 
Connection: Keep-Alive 
User-Agent: 

我讀了http.IncomingMessage文檔,但沒有方法一下子讓所有的主要價值。最接近的是

console.log(req.method, req.url, req.httpVersion, req.headers); 

與輸出

Server running at http://127.0.0.1:1337/ 
GET/1.1 { host: '127.0.0.1:1337', 
    connection: 'Keep-Alive', 
    'user-agent': 'RestClient-Tool' } 

完全req對象

{ _readableState: 
    { highWaterMark: 16384, 
    buffer: [], 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: false, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    calledRead: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    objectMode: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
    readable: true, 
    domain: null, 
    _events: {}, 
    _maxListeners: 10, 
    socket: 
    { _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: [Object], 
     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: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [] }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    errorEmitted: false, 
    bytesRead: 798, 
    _bytesDispatched: 156, 
    _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:127.0.0.1:1337' }, 
    _idleTimeout: 120000, 
    _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idleStart: 1413789077709, 
    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], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _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: [Object], 
     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: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [] }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    errorEmitted: false, 
    bytesRead: 798, 
    _bytesDispatched: 156, 
    _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:127.0.0.1:1337' }, 
    _idleTimeout: 120000, 
    _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idleStart: 1413789077709, 
    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], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular] } }, 
    httpVersion: '1.1', 
    complete: false, 
    headers: 
    { host: '127.0.0.1:1337', 
    connection: 'keep-alive', 
    'access-control-request-method': 'POST', 
    origin: 'null', 
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36', 
    'access-control-request-headers': 'accept, content-type', 
    accept: '*/*', 
    'accept-encoding': 'gzip,deflate,sdch', 
    'accept-language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4' }, 
    trailers: {}, 
    _pendings: [], 
    _pendingIndex: 0, 
    url: '/', 
    method: 'OPTIONS', 
    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: [Object], 
     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: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [] }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    errorEmitted: false, 
    bytesRead: 798, 
    _bytesDispatched: 156, 
    _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:127.0.0.1:1337' }, 
    _idleTimeout: 120000, 
    _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] }, 
    _idleStart: 1413789077709, 
    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], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular] } }, 
    _consuming: false, 
    _dumped: false, 
    httpVersionMajor: 1, 
    httpVersionMinor: 1, 
    upgrade: false } 

相關Per request logging in Node.js

+0

什麼是你的問題? – 2014-10-20 07:43:23

+0

它會更短嗎? – 2014-10-20 07:54:26

+0

這是關於如何格式化字符串的問題嗎?肯定沒有內置的方法來獲得你想要的輸出。 – jgillich 2014-10-20 07:58:20

回答

2
console.log(req.method, req.url, 'HTTP/' + req.httpVersion); 
for (var name in req.headers) 
    console.log(name + ':', req.headers[name]);