关于nodejs中函数参数的疑问
发布于 12 年前 作者 biguant 15089 次浏览 最后一次编辑是 8 年前

举个最简单的例子 http.createServer(function(request,response){});

我查看http模块是有createServer这个函数,也知道它有个参数 http.createServer([requestListener]) The requestListener is a function which is automatically added to the ‘request’ event.

问题是 我怎么知道里面这个函数的参数(我怎么知道似乎request和response,两个参数)有哪些,从

哪里的帮助文档可以看到

谢谢哈

7 回复

你直接看auguments即可,如下: createServer(function(){ console.log(arguments); });

这招屡试不爽啊~

{ ‘0’: { socket: { _handle: [Object], _pendingWriteReqs: 0, _flags: 0, _connectQueueSize: 0, destroyed: false, bytesRead: 328, bytesWritten: 0, allowHalfOpen: true, writable: true, readable: true, server: [Object], ondrain: [Function], _idleTimeout: 120000, _idleNext: [Object], _idlePrev: [Object], _idleStart: Tue, 28 Feb 2012 07:31:28 GMT, _events: [Object], ondata: [Function], onend: [Function], _httpMessage: [Object] }, connection: { _handle: [Object], _pendingWriteReqs: 0, _flags: 0, _connectQueueSize: 0, destroyed: false, bytesRead: 328, bytesWritten: 0, allowHalfOpen: true, writable: true, readable: true, server: [Object], ondrain: [Function], _idleTimeout: 120000, _idleNext: [Object], _idlePrev: [Object], _idleStart: Tue, 28 Feb 2012 07:31:28 GMT, _events: [Object], ondata: [Function], onend: [Function], _httpMessage: [Object] }, httpVersion: ‘1.1’, complete: false, headers: { host: ‘localhost:8124’, ‘user-agent’: ‘Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1’, accept: ‘text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8’, ‘accept-language’: ‘zh-cn,zh;q=0.5’, ‘accept-encoding’: ‘gzip, deflate’, ‘accept-charset’: ‘GB2312,utf-8;q=0.7,*;q=0.7’, connection: ‘keep-alive’ }, trailers: {}, readable: true, url: ‘/’, method: ‘GET’, statusCode: null, client: { _handle: [Object], _pendingWriteReqs: 0, _flags: 0, _connectQueueSize: 0, destroyed: false, bytesRead: 328, bytesWritten: 0, allowHalfOpen: true, writable: true, readable: true, server: [Object], ondrain: [Function], _idleTimeout: 120000, _idleNext: [Object], _idlePrev: [Object], _idleStart: Tue, 28 Feb 2012 07:31:28 GMT, _events: [Object], ondata: [Function], onend: [Function], _httpMessage: [Object] }, httpVersionMajor: 1, httpVersionMinor: 1, upgrade: false }, ‘1’: { output: [], outputEncodings: [], writable: true, _last: false, chunkedEncoding: false, shouldKeepAlive: true, useChunkedEncodingByDefault: true, _hasBody: true, _trailer: ‘’, finished: false, socket: { _handle: [Object], _pendingWriteReqs: 0, _flags: 0, _connectQueueSize: 0, destroyed: false, bytesRead: 328, bytesWritten: 0, allowHalfOpen: true, writable: true, readable: true, server: [Object], ondrain: [Function], _idleTimeout: 120000, _idleNext: [Object], _idlePrev: [Object], _idleStart: Tue, 28 Feb 2012 07:31:28 GMT, _events: [Object], ondata: [Function], onend: [Function], _httpMessage: [Circular] }, connection: { _handle: [Object], _pendingWriteReqs: 0, _flags: 0, _connectQueueSize: 0, destroyed: false, bytesRead: 328, bytesWritten: 0, allowHalfOpen: true, writable: true, readable: true, server: [Object], ondrain: [Function], _idleTimeout: 120000, _idleNext: [Object], _idlePrev: [Object], _idleStart: Tue, 28 Feb 2012 07:31:28 GMT, _events: [Object], ondata: [Function], onend: [Function], _httpMessage: [Circular] }, _events: { finish: [Function] } } }

我把req和res去掉了,打印的这些参数里,没有看到有 request和response这样的呢

这个疑问是由于文档没有清晰的标注出来,没有类型标注,呵呵。

其实仔细的阅读以下http这个文档: http://nodejs.org/docs/latest/api/http.html#http.ServerRequest

http://nodejs.org/docs/latest/api/http.html#http.ServerResponse

这是 response的描述 This object is created internally by a HTTP server–not by the user. It is passed as the second parameter to the ‘request’ event.

上面两个连接就是这两个对象的文档,requestListener ()的两个参数,分别就是他们的实例,所以都能查到,只是更详细的,比如request.headers 有什么,就需要自己 console.log一下了。

类似的文档中还有 fs.stat(path,function(err,stats){}) 这个地方callback的参数中stats 文档中尊美有对这个对象的描述的。

所以,这是node稳定的编写方式,适应这种方式,尽可能看全整个文档。这样会有很多细节的问题,都能被一一发现,:)

主要是看了些demo,里面回调函数会有些参数不知道哪里来的; 比如node里很多api的参数,可能就不知道包含哪些,导致调api的时候,无从下手,该从哪里获得数据。

LS说的是,这种编写方式确实不能通过只看单个api就能知道这个api里有哪些参数可用;

受教了

文档不完善。 有些时候需要自己去看代码来理解具体怎么用,的确比较累。

开始肯定对api会有各种不熟悉,写上几次 createServer() 后,你一辈子都记得了。

而且nodejs的其他类似的模块都会是一个道理。就如 callback(err, data) ,回调函数第一个都约定是 error

文档不全真是害死人啊。。。 用arguments的都是js fans,否则谁知道这种东东。。。

回到顶部