Fork me on GitHub

Node.js学习 - Function


Node.js函数和JavaScript类似

function say(word) {
  console.log(word);
}

function execute(someFunction, value) {
  someFunction(value);
}

execute(say, "Hello");

 

匿名函数

function execute(someFunction, value) {
  someFunction(value);
}

execute(function(word){ console.log(word) }, "Hello");

 

函数传递是如何让HTTP服务器工作的

var http = require("http");

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


也可以这样:
var http = require("http");

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

http.createServer(onRequest).listen(8888);

 

 

 

 

 

 


 

posted @ 2016-04-15 22:41  Roronoa__Zoro  阅读(177)  评论(0编辑  收藏  举报