node.js函数详解,如下:
普通函数:
function 函数名1(参数) {
console.log(参数);
}
使用函数作为参数:
function 函数名2(someFunction, value) {
someFunction(value);
}
调用函数:函数名2(函数名1, "值");
匿名函数:函数名2(function(word){ console.log(word) }, "值");
使用匿名函数创建http服务器:
//请求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);
使用普通函数创建http服务器:
//请求http
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);