首页 > 解决方案 > 为什么在下面的示例中命名中间件

问题描述

我正在从以下链接http://expressjs.com/en/guide/routing.html阅读有关路由器的文档

var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog (req, res, next) { //why is the middle ware named here
  console.log('Time: ', Date.now())
  next()
})
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
  res.send('About birds')
})

module.exports = router  

我怀疑(检查代码中的注释)为什么在 router.use(.....) 中间件被命名为 ie。它命名为timeLog,而通常我们不会在中间件中写名字,而是直接写函数(req,res)..我是这个领域的初学者,所以我可能会问一些明显的问题..但无论如何提前谢谢。

标签: node.jsexpressmiddleware

解决方案


推荐阅读