我在 NodeJS 中编写了一个代码,当我点击 url 时,我需要服务器通过三个中间件函数身份验证、cookie、日志记录。在这里它发生了,但控制台打印了两次。你能帮我弄清楚原因吗。
var express = require('express');
var app = express();
var router = require('express').Router();
/* Add the middleware to express app */
app.use (function authentication(req,res,next){
if(req.method === 'GET'){
console.log("Inside Authentication.js")
next(); // If your don't use next(), the mmand won't go to the next function.
}
else{
console.log("Inside else Authentication.js")
}
})
app.use( function cookies(req,res,next){
if (req.method === 'GET'){
console.log("Inside cookies.js")
next();
}
else
{
console.log("Inside else cookies.js")
}
})
app.use( function logging(req,res,next){
if(req.method === 'GET'){
console.log("Inside …Run Code Online (Sandbox Code Playgroud)