You generally only listen and wait for the "end" event on the request body
if you care about the request body.  In both these examples you're not
listening for the request "data" events, so I don't think the "end" event
(part of the same stream interface) is interesting.

Also I noticed that one example calls res.write(...) ; res.end(), while the
second does just res.end(...).  In node it's better to just call
res.end(...) for your last or only response body chunk.

I would write these two examples as:

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



On Thu, Feb 14, 2013 at 6:14 PM, Saulo Moraes <saulo.a.mor...@gmail.com>wrote:

> hi,
>
>   I am new to nodeJS and I am reading some examples to get started and I
> found 2 samples of webserver:
>
> sample 1)
>
> var http = require("http");
> http.createServer(function(request, response) {
>   response.writeHead(200, {"Content-Type": "text/plain"});
>   response.write("Hello World");
>   response.end();
> }).listen(8888);
>
> sample 2)
>
> var http = require("http");
> http.createServer(function (request, response) {
>    request.on("end", function () {
>       response.writeHead(200, {"Content-Type": "text/plain"});
>       response.end("Hello World");
>    });
> }).listen(8888);
>
>
> My doubt is what is that "request.on(.." in the second samples? What is the 
> best approach 1 or 2?
>
>
>
> thx,
> sauLo
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to