节点js中req和res对象的属性和方法

时间:2012-09-27 10:09:09

标签: node.js express

节点js中可用的响应和请求对象的属性和方法是什么?

赞:request.urlres.endres.write

我在哪里可以找到所有列出的属性?感谢

4 个答案:

答案 0 :(得分:2)

在JavaScript中查找函数,属性......对象的最简单方法是将对象序列化为字符串并将该字符串打印到控制台。

基本上这是一个单行,例如:

console.log(JSON.stringify(req));

这会显示req对象的所有内容。

答案 1 :(得分:1)

答案 2 :(得分:1)

为了最好地理解nodejs固有的模块之一,您应该首先阅读其文档。然后,如果需要进一步理解,您将需要探索定义它的源代码。 (大多数内在模块都是用JavaScript编写的。)

例如,ServerResponse对象(通常作为“res”传递给HTTP请求处理程序)在http.js中的第867行定义,可以在https://github.com/joyent/node/blob/master/lib/http.js

的nodejs源代码中找到

通过探索源代码,您将能够自己查看对象支持的方法和属性。

然后,要完全了解对象的工作原理并发现其所有的方法和属性,您需要遵循其继承树。

  

ServerResponse继承自

     
    

OutgoingMessage(在同一文件中定义),它本身继承自

         
      

Stream(在stream.js中定义)进一步继承自

             
        

events.EventEmitter,当然是

                 
          

对象

        
      
    
  

因此,ServerResponse的方法和属性的完整列表是:

ServerResponse

  statusCode             property
  _implicitHeader()      method
  assignSocket()         method
  detachSocket()         method
  writeContinue()        method
  writeHead()            method
  writeHeader()          method

  _buffer()              (from OutputMessage) method
  _finish()              (from OutputMessage) method
  _flush()               (from OutputMessage) method
  _renderHeaders()       (from OutputMessage) method
  _send()                (from OutputMessage) method
  _storeHeader()         (from OutputMessage) method
  _writeRaw()            (from OutputMessage) method
  addTrailers()          (from OutputMessage) method
  destroy()              (from OutputMessage) method
  end                    (from OutputMessage) method
  getHeader()            (from OutputMessage) method
  removeHeader()         (from OutputMessage) method
  setHeader()            (from OutputMessage) method
  write()                (from OutputMessage) method

  pipe()                 (from Stream) method

  addListener()          (from EventEmitter) method
  emit()                 (from EventEmitter) method
  listeners()            (from EventEmitter) method
  on()                   (from EventEmitter) method
  once()                 (from EventEmitter) method
  removeAllListeners()   (from EventEmitter) method
  removeListener()       (from EventEmitter) method
  setMaxListeners()      (from EventEmitter) method

  arguments              (from Object) property
  caller                 (from Object) property
  length                 (from Object) property
  name                   (from Object) property
  prototype              (from Object) property
  super_                 (from Object) property

  __defineGetter__()     (from Object) method
  __defineSetter__()     (from Object) method
  __lookupGetter__()     (from Object) method
  __lookupSetter__()     (from Object) method
  constructor()          (from Object) method
  hasOwnProperty()       (from Object) method
  isPrototypeOf()        (from Object) method
  propertyIsEnumerable() (from Object) method
  toLocaleString()       (from Object) method
  toString()             (from Object) method
  valueOf()              (from Object) method
  apply()                (from Object) method
  bind()                 (from Object) method
  call()                 (from Object) method

通过跟踪对象的整个继承树,您可以深入了解nodejs的工作原理,并欣赏它是一个多么神奇的工具。

答案 3 :(得分:0)

您可以在任何Node.js手册网站上找到许多列出的属性,例如http://nodemanual.org/latest/nodejs_dev_guide/creating_an_http_server.html