What are the properties and methods of response and request objects available in node js?
Like : request.url
or res.end
, res.write
?
Where can I find all the listed properties? Thanks
The easiest way to find out in JavaScript which functions, properties, ... an object has is to serialize the object to a string and print that string to the console.
Basically this is a one-liner, such as:
console.log(JSON.stringify(req));
This shows you everything the req
object has.
To best understand one of the modules intrinsic to nodejs, you should first read its documentation. Then, if further understanding is required, you'll want to explore the source code where it is defined. (Most of the intrinsic modules are themselves written in JavaScript.)
For example, the ServerResponse object (as typically passed into HTTP request handlers as "res") is defined on line 867 in http.js which can be found in the nodejs source code at https://github.com/joyent/node/blob/master/lib/http.js
By exploring the source, you'll be able to see what methods and properties the object supports by itself.
Then, to fully understand how a object works and discover all of its methods and properties, you'll need to follow its inheritance tree.
ServerResponse inherits from
OutgoingMessage (defined in the same file) which itself inherits from
Stream (defined in stream.js) which further inherits from
events.EventEmitter, which is of course, an
Object
So, the full list of methods and properties of ServerResponse are:
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
By following an object's entire inheritance tree, you build an intimate understanding of how nodejs works, and appreciate what an amazing tool it is.
You can find a lot of the properties listed at any of the Node.js manual websites, such as http://nodemanual.org/latest/nodejs_dev_guide/creating_an_http_server.html