当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 实例详解Node.js 函数

实例详解Node.js 函数

2018年08月12日  | 移动技术网IT编程  | 我要评论

内容:普通函数,匿名函数,函数传递是如何让http服务器工作的

###普通函数

例子:

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服务器工作的

带着这些知识,我们再来看看我们简约而不简单的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);

现在它看上去应该清晰了很多:我们向 createserver 函数传递了一个匿名函数。

用这样的代码也可以达到同样的目的:

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);

总结

以上所述是小编给大家介绍的node.js 函数,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网