首页 > 其他 > 详细

4.创建中间件

时间:2016-11-14 16:48:23      阅读:111      评论:0      收藏:0      [点我收藏+]

创建自定义中间件

提供一个 接受Request对象作为第一个参数,Response对象作为第二个参数,next作为第三个参数 的函数

next()参数是一个通过中间件框架传递的函数,指向下一个要执行的中间件函数。所以必须在退出自定义函数之前调用next(),否则程序不会被调用

var express=require(‘express‘);
var app=express();
function queryRemover(req,res,next){
	console.log("\n Before URL: ");
	console.log(req.url);
	req.url=req.url.split(‘?‘)[0];
	console.log("\n After URL: ");
	console.log(req.url);
	next();
}
app.use(queryRemover);
app.get(‘/query‘,function(req,res){
	res.send("test");
})
app.listen(8081);

  

4.创建中间件

原文:http://www.cnblogs.com/weizaiyes/p/6062175.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!