简体   繁体   中英

Node.js Simple Server JavaScript

I just started last week learning JavaScript and Node.js. Before that I developed with Java WebObjects and VB.NET. I just want to learn it for my self.

My brain is hurting after this week because of closures and other JavaScript stuff.

And now the question. To create a simple Node server I always found some code like this.

var http = require("http");

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

Is there any difference if I would write the code like this?

var http = require("http");


var serverCallback = function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write("Hello World");
    response.end();
}

var server = http.createServer(serverCallback);
server.listen(3000);

For me this is more readable. But I'm not really sure that its exact the same.

There is no difference in functionality. Use whatever style you like.

在这种情况下,唯一的区别是变量的分配方式,昨天在HN https://news.ycombinator.com/item?id=7672131中找到了

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM