您的位置:首页 > 其它

Connect中间件——可配置的中间组件

2016-03-13 21:21 387 查看
logger.js

var connect = require('connect');
var app = connect()
.use(setup(':method :url'))
.use(hello)
.listen(3000);

function hello(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('hello world');
}

/* 可配置的Connect中间件组件 */
function setup(format) {
var regexp = /:(\w+)/g;
return function logger(req, res, next) {
// 用正则表达式格式化请求的日志条目
var str = format.replace(regexp, function(match, property) {
return req[property];
});
console.log(str);
next();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: