Node.js请求对象文档?[关闭]


80

这是一个非常简单的问题,但是我还没有在Google上找到任何东西。我正在寻找有关Node.js的创建服务器功能中的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 }

53
你有我的同情。大多数现代语言,库和框架都有令人震惊的参考文档。如果您访问下面提到的节点文档,您会发现它实际上根本不是参考。更多的是讨论。例如,createServer()文档说它以参数“自动添加到'request'事件的函数”作为参数,并返回“ Web服务器对象”。没有任何暗示这些东西是什么“类型”,当然也没有指向这些东西的参考文档的链接。这些是什么?F减去文档。
jarmod

1
我通读了这个问题,对我来说似乎很合理。这里的问题在于,是从一个问题到对可怕文档的对话/可怜方的回应。如果可以给出合理的答案/解释,我认为这个问题是可以解决的。也许甚至是一个新的相关问题也是最好的。
demongolem

我六年前问过这个问题。从那时起,我在阅读Nodejs文档方面获得了更多经验,并且曾经从事Nodejs文档工作的人都在保持其最新性和准确性方面做了大量工作。我认为这个问题已经引起了很多关注,因为这是一个简单的问题,但是它表明,如果您被某些问题困扰,可以通过console.log获得很多信息。
本杰明·柯林斯

Answers:


26

我不确定这是否是正确的答复,因为这对我来说似乎很简单,但是您是否看过 http://nodejs.org/api/http.html#http_http_incomingmessage

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

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

希望能帮助到你。


糟糕,我的意思是json.stringify,而不是json.parse。回到尝试您提到的for(i in request)方法,由于某种原因,只做了console.log(request)即可工作。我希望它只是说[Object object]。也许它具有用于控制台的特定toString函数?谢谢回复。
本杰明·柯林斯

5
更好的是,要查看任何对象内部的内容,请使用util.inspect()。Util是一个核心模块;您可以简单地var util=require('util');util.inspect(obj)。一个基本的问题JSON.stringify()是,如果有循环引用,它将抱怨。
塔利斯K.13年

2
_readableStatereadabledomain_events_maxListenerssocketconnectionhttpVersioncompleteheaderstrailers_pendings_pendingIndexurlmethodstatusCodeclient_consuming_dumpedhttpVersionMajorhttpVersionMinorupgradesetTimeoutread_readdestroy_addHeaderLine_dumppushunshiftsetEncodingpipeunpipeonaddListenerresumepausewrapsetMaxListenersemitonceremoveListenerremoveAllListenerslisteners
CaffeineAddiction


10

补充一下,作为Node.js中的一个相对新手,我发现每个框架都有自己的“请求”对象实现令人困惑。

例如,

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

Hapi:http : //spumko.github.io/resource/api/#request-object(仅注意原始请求对象在request.raw.req中仍然可用)

一些库假定存在Express的“ req.param”方法,并且在非Express项目中使用时会失败。


非常感谢您分享express api选项。
Muhammad Raihan Muhaimin 2014年

spumko链接已损坏。
Charles Holbrow 2014年


解析网址为404,还有
Jon Biz

为什么要使用http“框架”?Node.js本地执行http。
英国电信

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.