5

I've decided to learn node, an so I'm following, to begin with, The Node Beginner Book. As in I guess a lot of other resources, there is the "simple HTTP server", first step, something like:

var http = require("http");

http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(8888);

As I understand it, when someone, in this case me though localhost:8888, makes a request, an event is triggered, and the anonymous function that got passed to http.createServer gets fired. I put here the documentation that I've managed to find about http.createserver for anyone that finds it useful:

http.createServer([requestListener])

Returns a new web server object.

The requestListener is a function which is automatically added to the 'request' event.

(from the node.js site)

I couldn't find or figure out through how does this triggered function get it's parameters passed, and how do I find out about it. So... how do I know where does these parameters come from, what methods do they offer, etc?

Thanks in advance!

Community
  • 1
  • 1
ferhtgoldaraz
  • 1,693
  • 3
  • 15
  • 20
  • 2
    http://nodejs.org/api/http.html#http_http_createserver_requestlistener `event: 'request'` right below where that link ends up. It's defined by node.js itself. – Marc B Dec 05 '13 at 20:37
  • The Node.js documentation isn't really great until you get familiar with Node. There have been many times where I just refer to the source code to figure it out. Once you get familiar with it though, it isn't so bad. – Brad Dec 05 '13 at 20:39
  • D'oh. How could I lose this much time looking for something so in front of me! Thanks. If you make it an answer I will accept it right away. [Here](http://nodejs.org/api/http.html#http_event_request) is another link to it, a bit more right to the point I think. – ferhtgoldaraz Dec 05 '13 at 20:42
  • The docs mentioned by Marc B are the way to go, but they are often a bit thin. Reminds me of man pages in that they get you pointed the right direction but often neglect key information. Or assume you know what you are doing :-) I'm finding the inexpensive Leanpub on-line Node books reasonably useful too. – user949300 Dec 05 '13 at 20:50

3 Answers3

2

In JavaScript, functions can be passed into methods as a parameter. Example:

function funcA(data) {
    console.log(data);
}
function funcB(foo) {
    foo('I'm function B');    // Call 'foo' and pass a parameter into that function
}
funcB(funcA); // Pass funcA as a parameter into funcB

What you're doing with http.createServer is the above, passing a function that can accept parameters. A new server expects you to pass in a function that it can call. The server will do internal actions which it will create a request and response object, and then call the function you passed in with those variables.

Read about the Http Event: Request for details about these parameters.

matth
  • 6,112
  • 4
  • 37
  • 43
1

this should be the create stack: https://github.com/joyent/node/blob/master/lib/http.js#L62 > https://github.com/joyent/node/blob/master/lib/_http_server.js#L253 so if a request is fired, this should be get triggered: https://github.com/joyent/node/blob/master/lib/_http_server.js#L502 - or maybe this: https://github.com/joyent/node/blob/master/lib/_http_server.js#L505

jko
  • 2,068
  • 13
  • 25
  • Maybe I'm too noob, but this is still too puzzling for me. – ferhtgoldaraz Dec 05 '13 at 20:49
  • you wondered how your function gets the params? the function you defined is passed over to createServer() - which calls Server() - which adds your function as an event listener and calls it on one of the last two linked lines. and at this point of call via self.emit('request', req, res) the res and req are passed over to your function as parameters. – jko Dec 05 '13 at 20:53
  • Nice, but that would still itch me to find out what are `req` and `res` being, though I will try and read the source too, so I start to get the much needed ability to understand it fully. I now think I know what they are though. – ferhtgoldaraz Dec 05 '13 at 21:00
0

The node.js documentation, explains pretty much everything you need to know about a http.ClientRequest and a http.ServerResponse, including methods and events.

If you need information about the HTTP protocol in general, you can find a lot of resources by googling it, such as the HTTP Wikipedia page.

If you want to see in details how HTTP is implemented in node, you'll have to jump into the node.js source code.

Also, you might be interested in express.js, which the most used web framework for node, hence a lot of resources about it are available online.

Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
  • Thanks, I was actully, a bit furher down the book, getting intrested by the first paramter, that I now believe is an instance of `http.IncomingMessage` – ferhtgoldaraz Dec 05 '13 at 20:55