node js中可用的response和request对象的属性和方法有哪些?
喜欢:request.url
或res.end
,res.write
?
我在哪里可以找到所有列出的属性?谢谢
在 JavaScript 中找出对象具有哪些函数、属性……的最简单方法是将对象序列化为字符串并将该字符串打印到控制台。
基本上这是一个单行,例如:
console.log(JSON.stringify(req));
这向您展示了该req
对象所拥有的一切。
为了更好地理解 nodejs 固有的模块之一,您应该首先阅读它的文档。然后,如果需要进一步了解,您将需要探索定义它的源代码。(大多数内部模块本身都是用 JavaScript 编写的。)
例如,ServerResponse 对象(通常作为“res”传递到 HTTP 请求处理程序)在 http.js 的第 867 行定义,可以在https://github.com/joyent/node/的 nodejs 源代码中找到blob/master/lib/http.js
通过探索源代码,您将能够看到对象本身支持哪些方法和属性。
然后,要充分了解对象的工作原理并发现其所有方法和属性,您需要遵循其继承树。
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 的工作原理,并欣赏它是一个多么了不起的工具。
您可以在任何 Node.js 手册网站上找到许多列出的属性,例如http://nodemanual.org/latest/nodejs_dev_guide/creating_an_http_server.html