为什么中间件的内容在浏览器的单个请求中是 运行 四次

Why content of middleware is running four times on single request from browser

为什么每个请求 console.log('First Log') 运行 4 次?

//app.js
const express = require('express');
var app = express();

app.use((req, res, next) => {
  console.log('First Log'); // problem is here
  next();
});

app.use((req, res, next) => {
  res.send('first response from express');
});

module.exports = app;
//server.js
const http = require('http');
const app  = require('./backend/app');

var port =  process.env.PORT || 3000;

app.set('port', port);

var server = http.createServer(app);
server.listen(port);

输出:

First Log
First Log
First Log
First Log

中间件可以对所有路径通用,或者仅在您的服务器处理的特定路径上触发。下面是一个中间件声明的例子。

var app = express();
app.use(function () {}) //added to all paths or globally
app.get('/someroute', function() {}) //added to a specific path

参考: https://medium.com/@agoiabeladeyemi/a-simple-explanation-of-express-middleware-c68ea839f498

@AikonMogwai 评论中提到的答案也是正确的:

The middleware works one time for each url(of the same route path): index.html, favicon.ico, *.css, etc. 
Change console.log to console.log(req.url) to see that.