Nodejs结合javascript的闭包的一个例子
//server.js文件
var http = require('http');
var dispatcher = require('./dispatcher.js'); //dispatcher模块
//在这里我们就可以利用闭包,将一些环境变量设置下来,dispatcher.perform方法必须返回一个匿名function
var perform = dispatcher.perform({
"hello": "world"
});
http.createServer(function(request, response) {
//处理请求,perform必须为一个function,并且需要两个参数(request, response)
perform(request, response);
}).listen(80);
//******************华丽的分割线*******************
//dispatcher.js文件
//定义perform,利用闭包的特性绑定了vars变量
exports.perform = function(vars) {
return function(request, response) {
response.end(JSON.stringify(vars));
}
}
Nodejs结合javascript的闭包的一个例子
原创
©著作权归作者所有:来自51CTO博客作者weekface的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Vsftpd虚拟用户设置
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
实验记录:Tomcat - 制作和部署一个war包Tomcat war包 部署war包 制作war包
-
var居然输出6,一个例子带你辨别闭包陷阱
当循环运行时,会先设置五个不同的定时器,但是它们都是在循环结束后的 1000 毫秒之后才执行由于循
vue.js javascript java 作用域 回调函数 -
Golang的interface一个例子
我们用Gola
Web HTTP 语言类