middleware logger() is called twice if I put it as a first argument to app.use()
This happens because the browser is requesting two files. The first will be for / and the second is likely for favicon.ico. If you want to see why this is being called change your one function to look like this:
function logger(req, res, next) {
console.log('This is middleware', req.originalUrl);
next();
}
Then it will output the URL that was requested for each time the browser hits the server.
it doesn't get called at all if put as a second argument
Because request has already been served when express reaches the logger middleware, order of middeware is very important in express.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…