Node.js 如何在express中创建自定义中间件?
我需要在快递定制中间件与路线承诺执行特定的任务。下面是需要输入代码的示例代码:Node.js 如何在express中创建自定义中间件?,node.js,express,promise,Node.js,Express,Promise,我需要在快递定制中间件与路线承诺执行特定的任务。下面是需要输入代码的示例代码: function middle(){ // some code; } app.get('/', middle, function (req, res) { console.log('url is working'); res.send("Chat server is working!"); }); 您的中间件函数需要有签名:function middleq,re
function middle(){
// some code;
}
app.get('/', middle, function (req, res) {
console.log('url is working');
res.send("Chat server is working!");
});
您的中间件函数需要有签名:function middleq,res,next,并且您需要在中间件函数的末尾调用next,以便它转到链中的下一个函数,因此根据您的示例,代码如下所示:
function middle(req, res, next){
// some code;
next();
}
app.get('/', middle, function (req, res) {
console.log('url is working');
res.send("Chat server is working!");
});