express框架---快递.路由器

快递.路由器

使用express.Router该类创建模块化的、可安装的路由处理程序。一个Router实例是一个完整的中间件和路由系统。
commom.js是创建的一个路由router文件,内容如下:

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

router.use(function timeLog (req, res, next) {
    
    
  console.log('Time: ', Date.now())
  next()
})
router.get('/', function (req, res) {
    
    
  res.send(' home 页面')
})
router.get('/about', function (req, res) {
    
    
  res.send(' About 页面')
})

module.exports = router

在应用程序中加载路由器模块 是在创建服务器的文件中写代码:

const commom= require('./commom') //require(文件地址)

// ...

app.use('/Commom', commom);      /Commom为路由名称

猜你喜欢

转载自blog.csdn.net/weixin_47863547/article/details/119490869