zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

4.创建中间件

2023-02-18 16:29:39 时间

创建自定义中间件

提供一个 接受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);