实例详解Node.js 函数
内容:普通函数,匿名函数,函数传递是如何让HTTP服务器工作的
###普通函数
例子:
functionsay(word){
console.log(word);
}
functionexecute(someFunction,value){
someFunction(value);
}
execute(say,"Hello");
###匿名函数
functionexecute(someFunction,value){
someFunction(value);
}
execute(function(word){console.log(word)},"Hello");
####################################################################################
函数传递是如何让HTTP服务器工作的
带着这些知识,我们再来看看我们简约而不简单的HTTP服务器:
varhttp=require("http");
http.createServer(function(request,response){
response.writeHead(200,{"Content-Type":"text/plain"});
response.write("HelloWorld");
response.end();
}).listen(8888);
现在它看上去应该清晰了很多:我们向createServer函数传递了一个匿名函数。
用这样的代码也可以达到同样的目的:
varhttp=require("http");
functiononRequest(request,response){
response.writeHead(200,{"Content-Type":"text/plain"});
response.write("HelloWorld");
response.end();
}
http.createServer(onRequest).listen(8888);
总结
以上所述是小编给大家介绍的Node.js函数,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短