title: How to log Node.js request
body: <p>How to log Node.js request? </p>
  
  <pre><code>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/');
  </code></pre>
  
  <p>For example, when using <code>console.log(req);</code>, I get long <code>req</code> object see below, but for logging it should much shorter like.</p>
  
  <pre><code>GET / HTTP/1.1
  Host: 127.0.0.1:1337
  Connection: Keep-Alive
  User-Agent: 
  </code></pre>
  
  <p>I read doc on <a href="http://nodejs.org/api/http.html#http_http_incomingmessage" rel="nofollow noreferrer">http.IncomingMessage</a> but there is no method to get all main values at once. The closest was</p>
  
  <pre><code>console.log(req.method, req.url, req.httpVersion, req.headers);
  </code></pre>
  
  <p>with output </p>
  
  <pre><code>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' }
  </code></pre>
  
  <p>Full <code>req</code> object</p>
  
  <pre><code>{ _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 }
  </code></pre>
  
  <p>related to <a href="https://stackoverflow.com/questions/19841410/per-request-logging-in-node-js">Per request logging in Node.js</a></p>