Node.js请求对象文档? [英] Node.js request object documentation?

查看:129
本文介绍了Node.js请求对象文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很直接的问题,但Google还没有发现任何内容。我正在查找Node.js的创建服务器功能中的请求参数的文档,但是我还没有找到任何东西。

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

使用JSON.stringify()调试给我一个错误,对象是循环的,程序停止。我看到不同的东西,如 request.url request.body ,但是有一个页面记录所有请求功能和参数?似乎应该很容易找到,我似乎找不到。



我再试一次,结果只是在做$ code> console.log(request)写出请求中的所有数据。这里只是作为参考:

  ondata:[功能],
_httpMessage:
{domain :null,
_events:[Object],
_maxListeners:10,
output:[],
outputEncodings:[],
可写:true,
_last:false,
chunkedEncoding:false,
shouldKeepAlive:true,
useChunkedEncodingByDefault:true,
sendDate:true,
_hasBody:true,
_trailer :
完成:
_hangupClose:false,
套接字:[循环],
连接:[循环]}},
连接:
{_connecting:false,
_handle:
{fd:null,
writeQueueSize:0,
owner:[Circular],
onread:[Function:onread ],
阅读:true},
_readableState:
{highWaterMark:16384,
缓冲区:[],
长度:0,
管道:null,
pipesCount:0,
flows:false,
ends:false,
endEmitted:false,
reading:true,
calledRead:
sync:false,
needReadable:true,
emitReadable:false,
readableListening:false,
objectMode:false,
defaultEncoding:'utf8 ',
ranOut:false,
awaitDrain:0,
readingMore:false,
decoder:null,
encoding:null},
可读:true ,
domain:null,
_events:
{end:[Object],
finish:[Function:onSocketFinish],
_socketEnd:[Function:onSocketEnd]
drain:[功能:ondrain],
超时:[功能],
错误:[功能],
关闭:[对象]},
_maxListeners:10 ,
_writableState:
{highWaterMark:16384,
objectMode: false,
needDrain:false,
结束:false,
结束:false,
完成:false,
decodeStrings:false,
defaultEncoding:'utf8 ',
length:0,
writing:false,
sync:true,
bufferProcessing:false,
onwrite:[功能],
writecb: null,
writelen:0,
buffer:[]},
可写:true,
allowHalfOpen:true,
onend:[功能],
被破坏: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,
超时:120000,
_connectionKey:'4:0.0.0.0:1337'},
_idleTimeout:120000,
_idleNext:
{_idleNext:[Circular],
_idlePrev:[Circular],
msecs:120000,
ontimeout:[功能:listOnTimeout]},
_idlePrev:
{_idleNext:[Circular],
_idlePrev:[Circular],
msecs:120000,
ontimeout:[功能:listOnTimeout]},
_idleStart:1371952289619,
解析器:
{_headers:[],
_url:'',
onHeaders:[功能:parserOnHeaders],
onHeadersComplete:[功能:parserOnHeadersComplete],
onBody:[功能:parserOnBody],
onMessageComplete:[功能:parserOnMessageComplete],
套接字:[Circular],
incoming:[Circular],
maxHeaderPairs:2000,
onIncoming:[功能]},
ondata:[功能],
_httpMessage:
{domain:null,
_events:[Object],
_maxListeners:10,
output:[] ,
outputEncodings:[],
可写: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,像
Gecko) rome / 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,
客户端:
{_connecting:false,
_handle:
{fd:null,
writeQueueSize:0,
所有者:[Circular],
onread:[function:onread],
reading:true},
_readableState:
{highWaterMark:16384,
buffer :[],
length:0,
pipes:null,
pipesCount:0,
flows:false,
ends:false,
endEmitted: false,
阅读:true,
calledRead:true,
sync:false,
needReadable:true,
emitReadable:false,
readableListening:
objectMode:false,
defaultEncoding:'utf8',
ranOut:false,
awaitDrain:0,
readingMore:false,
decoder:null,
encoding:null} ,
可读:true,
domain:null,
_events:
{end:[Object],
finish:[Function:onSocketFinish],
_socketEnd:[功能:onSocketEnd],
drain:[功能:ondrain],
超时:[功能],
错误:[功能],
关闭:[对象]} ,
_maxListeners:10,
_writableState:
{highWaterMark:16384,
objectMode:false,
needDrain:false,
ends:false,
结束:false,
完成:false,
decodeStrings:false,
defaultEncoding:'utf8',
长度:0,
写入:false,
sync:true,
bufferProcessing:false,
onwrite:[功能],
writecb:null,
writelen:0,
buffer:[]},
可写:true,
allowHalfOpen:true,
onend:[功能],
destroy:false,
errorEmitted:false,
bytesRead:357,
_bytesDispatched:0,
_pendingData:null,
_pendingEncoding:'',
服务器:
{domain:null,
_events:[Object],
_maxListeners:10,
_connections:1,
connections:[Getter / Setter]
_handle:[Object],
_usingSlaves:false,
_slaves:[],
allowHalfOpen:true,
httpAllowHalfOpen:false,
超时:120000 ,
_connectionKey:'4:0.0.0.0:1337'},
_idleTimeout:120000,
_idleNext:
{_idleNext:[Circular],
_idlePrev: Circular],
msecs:120000,
ontimeout:[功能:listOnTimeout]},
_idlePrev:
{_idleNext:[Circular],
_idlePrev:[Circular],
msecs:120000,
ontimeout:[功能:listOnTimeout]},
_idleStart:1371952289619,
parser:
{_headers:[],
_url:'',
onHeaders:[功能:parserOnHeaders],
onHeadersComplete:[功能:parserOnHeadersComplete],
onBody:[功能:parserOnBody],
onMessageComplete:[功能:parserOnMessageComplete],
套接字:[Circular],
incoming:[Circular],
maxHeaderPairs:2000,
onIncoming:[功能]},
ondata:[功能],
_httpMessage:
{domain:null,
_events:[Object],
_maxListeners: 10,
输出:[],
outputEncodings:[],
可写:true,
_last:false,
chunkedEncoding:false,
shouldKeepAlive: true,
useChunkedEncod ingByDefault: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



请求是上述对象的实例。顺便说一下,如果你真的想用强力看到那个对象里面的内容,那么你不应该解析它,因为它不会是一个JSON字符串,而是做一些类似的事情:

  for(i in request){
//你的代码在这里
}
/ pre>

希望有帮助。


This is a pretty straight forward question, but I haven't found anything on Google. I'm looking for documentation on the request parameter in Node.js's create server function, but I haven't been able to find anything.

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

Debugging with JSON.stringify() gives me an error, that the object is circular and the program stops. I've seen different things like request.url, or request.body, but is there a page documenting all of the request functions and parameters? It seems like it should be easy to find, I just can't seem to find it.

I tried it again, and it turns out just doing console.log(request) writes out all of the data from the request. Here it is just as a reference:

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 }

解决方案

I'm not sure if this is a correct reply as it seems straightforward to me, but did you have a look at http://nodejs.org/api/http.html#http_http_incomingmessage?

Request is instance of the above object. By the way, if you really want to see what's inside that object with "brute force," you should not parse it, as it will not be a JSON string at all, but instead do something like:

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

Hope it helps.

这篇关于Node.js请求对象文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆