Node.js请求对象文档?

Node.js请求对象文档?,node.js,documentation,request,Node.js,Documentation,Request,这是一个非常直截了当的问题,但我还没有在谷歌上找到任何东西。我正在Node.js的create server函数中寻找关于request参数的文档,但是我没有找到任何东西 http.createServer(function(request, response){ console.log(JSON.stringify(request)); }); 使用JSON.stringify()进行调试时会出现一个错误,即对象是循环的,程序会停止。我见过不同的东西,比如request.url,或者re

这是一个非常直截了当的问题,但我还没有在谷歌上找到任何东西。我正在Node.js的create server函数中寻找关于request参数的文档,但是我没有找到任何东西

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 }

我不确定这是否是一个正确的回答,因为这对我来说似乎很简单,但你看了吗 ?

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

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

希望能有所帮助。

我同意Delio的观点,请求对象是由http.ClientRequest创建的IncomingMessage对象类型。

作为Node.js中的一个相对新手,我发现每个框架都有自己的“请求”对象实现,这让我感到困惑

比如说,

快递:

Hapi:(注意原始请求对象在request.raw.req中仍然可用)


有些库假设存在Express的“req.param”方法,在非Express项目中使用时失败。

Oops,我指的是json.stringify,而不是json.parse。返回尝试您提到的for(请求中的i)方法,出于某种原因,只需执行console.log(请求)即可。我希望它只是说,[对象]。也许它有一个特定的控制台toString功能?感谢您的回复。更好的是,要查看任何对象内部的内容,请使用util.inspect()。Util是一个核心模块;您可以简单地
var util=require('util')
util.inspect(obj)
JSON.stringify()
的一个基本问题是,如果存在循环引用,它会抱怨。
\u readableState
readable
事件
最大侦听器
套接字
连接
httpVersion
完成
标题
拖车
拖车
拖车
拖车
方法
状态码
客户端
消费
卸载
httpVersionMajor
httpVersionMinor
升级
设置超时
读取
读取
销毁
添加标题线
转储
推送
取消移位
设置编码
管道
打开
添加监听器
恢复
暂停
包裹
设置监听器,
一旦
移除监听器
移除所有监听器
监听器
链接已过时:我很同情你。大多数现代语言、库和框架都有令人震惊的参考文档。如果您访问下面提到的节点文档,您会发现它实际上根本不是一个参考;这更像是一场讨论。例如,createServer()文档表示它将“一个自动添加到‘请求’事件中的函数”作为参数,并返回“一个web服务器对象”。没有关于这两种东西的“类型”的提示,当然也没有指向这些东西的参考文档的链接。它们是什么?我把这个问题通读了一遍,对我来说似乎很合理。这里的问题是,正是这些回答将这一点从一个问题带到了一个关于可怕文档的对话/同情派对上。如果能给出合理的答案/解释,我认为这个问题是可以补救的。也许一个新的,相关的问题是最好的。我6年前问过这个问题。从那以后,我在阅读Nodejs文档方面获得了更多的经验,而从事Nodejs文档工作的人在保持文档的最新性和准确性方面做了大量工作。我认为这个问题已经引起了很多关注,因为这是一个简单的问题,但它表明,如果您遇到问题,可以通过console.log获得大量信息。非常感谢您共享express api选项。spumko链接已断开。解析文档比大多数文档更完整:解析url为404,fwiw您为什么要使用http“框架”?Node.js以本机方式执行http。