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