eikiro-000
eikiro-000

Reputation: 180

Node.js detailed documentation?

How come the documentation for things like http.IncomingMessage doesn't have all the attributes and methods?

I've seen tutorials use

http.createServer(function(request, response){
    request.url

but how does one know such attribute 'url' even exist?

The docs at http://nodejs.org/api/http.html#http_http_incomingmessage only gives a description of it.

Upvotes: 0

Views: 707

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 186984

Well, let's start with the http.createServer docs. http://nodejs.org/api/http.html#http_http_createserver_requestlistener

http.createServer([requestListener])

Returns a new web server object.The requestListener is a function which is automatically added to the 'request' event.

So then we look up the request event. http://nodejs.org/api/http.html#http_event_request

Event: 'request' function (request, response) { }

Emitted each time there is a request. Note that there may be multiple requests per connection (in the case of keep-alive connections). request is an instance of http.IncomingMessage and response is an instance of http.ServerResponse.

So now we know the call callback accepts request and response as arguments, and that request is an instance of IncomingMessage. So then we lookup IncomingMessage. http://nodejs.org/api/http.html#http_http_incomingmessage

http.IncomingMessage

An IncomingMessage object is created by http.Server or http.ClientRequest and passed as the first argument to the 'request' and 'response' event respectively. It may be used to access response status, headers and data.

It implements the Readable Stream interface, as well as the following additional events, methods, and properties.

So we scroll down a bit until we get to this: http://nodejs.org/api/http.html#http_message_url

message.url

Only valid for request obtained from http.Server.

Request URL string. This contains only the URL that is present in the actual HTTP request.

Seems pretty straight forward.

Upvotes: 4

Related Questions