Node.js 请求对象文档?

标签 node.js documentation request

这是一个非常直截了当的问题,但我在 Google 上没有找到任何内容。我正在查找有关 Node.js 的创建服务器函数中的请求参数的文档,但我找不到任何东西。

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

使用 JSON.stringify() 调试给我一个错误,即对象是循环的并且程序停止。我见过不同的东西,例如 request.urlrequest.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 }

最佳答案

我不确定这是否是一个正确的回复,因为这对我来说似乎很简单,但你有没有看过 http://nodejs.org/api/http.html#http_http_incomingmessage ?

Request 是上述对象的实例。顺便说一句,如果您真的想用“蛮力”查看该对象内部的内容,则不应解析它,因为它根本不是 JSON 字符串,而是执行以下操作:

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

希望对你有帮助。

关于Node.js 请求对象文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251553/

相关文章:

node.js - 在哪里查找 'cert' 和 'key' 来创建 https 服务器?

svn - Subversion 在存储大量二进制文件方面有多好?

java - 在soapui java API中加载外部请求文件

request - 使用 MQTT 的 RPC 样式请求

node.js - Sequelize PG : select with join, 订单,限制

node.js - 如何在 Linux 上为单个用户安装 node.js 模块(没有 root 权限)?

ruby - ruby-doc 和 apidock 有什么区别?

R和roxygen2 : How to document data files in inst/extdata?

node.js - 无法再在 request.jar 中添加 cookie

python - 无法使用 Python 和 Node 共同设计应用程序