一、Express中间件的概念与基本应用
const express = require('express')// 加一个注释,用以说明,本项目代码可以任意定制更改
const app = express()const PORT = process.env.PORT || 3000// // 挂载路由
// app.use('/api', router)// // 挂载统一处理服务端错误中间件
// app.use(errorHandler())app.get('/', (req, res) => {console.log(`${req.method}, ${req.url}, ${Date.now()}`);res.send('/index')
})app.get('/register', (req, res) => {console.log(`${req.method}, ${req.url}, ${Date.now()}`);res.send('/iregisterdex')
})app.get('/login', (req, res) => {console.log(`${req.method}, ${req.url}, ${Date.now()}`);res.send('/login')
})app.listen(PORT, () => {console.log(`Server is running at http://localhost:${PORT}`)
})
封装个方法:
const express = require('express')// 加一个注释,用以说明,本项目代码可以任意定制更改
const app = express()const PORT = process.env.PORT || 3000function logs(req) {console.log(`${req.method}, ${req.url}, ${Date.now()}`);console.log(`${req.method}, ${req.url}, ${Date.now()}`);console.log(`${req.method}, ${req.url}, ${Date.now()}`);
}app.get('/', (req, res) => {logs(req)res.send('/index')
})app.get('/register', (req, res) => {logs(req)res.send('/iregisterdex')
})app.get('/login', (req, res) => {logs(req)res.send('/login')
})app.listen(PORT, () => {console.log(`Server is running at http://localhost:${PORT}`)
})
发现也并不完美
写个中间件
const express = require('express')// 加一个注释,用以说明,本项目代码可以任意定制更改
const app = express()const PORT = process.env.PORT || 3000app.use((req, res, next) => {console.log(`${req.method}, ${req.url}, ${Date.now()}`);next()
})// // 挂载路由
// app.use('/api', router)// // 挂载统一处理服务端错误中间件
// app.use(errorHandler())// 中间件写在要使用的逻辑前面
app.get('/', (req, res) => {res.send('/index')
})app.get('/register', (req, res) => {res.send('/register')
})app.get('/login', (req, res) => {res.send('/login')
})app.listen(PORT, () => {console.log(`Server is running at http://localhost:${PORT}`)
})
二、不同中间件类别的使用方式
中间件分类:
- 应用程序级别中间件
- 路由级别中间件
- 错误处理中间件
- 内置中间件
- 第三方中间件
1、应用程序级别中间件
const express = require('express')
const app = express()const PORT = process.env.PORT || 3000// 以下3个都是应用程序级别中间件
app.use((req, res, next) => {console.log(`${req.method}, ${req.url}, ${Date.now()}`);next()
})app.get('/', (req, res) => {res.send('/index')
})app.get('/user', (req, res, next) => {console.log(req.method)next()
}, function (req, res, next){console.log('666')next()res.send('/user')
})app.listen(PORT, () => {console.log(`Server is running at http://localhost:${PORT}`)
})
2、路由级别中间件
router/index.js
const express = require('express')const router = express.Router()router.get('/', (req, res) => {console.log(req.methods)res.send('/index')
})router.get('/user', (req, res) => {console.log(req.methods)res.send('/user')
})module.exports = router
app.js
const express = require('express')
const app = express()
const router = require('./router/index')
const routerVideo = require('./router/video')const PORT = process.env.PORT || 3000// 挂载路由
// app.use(router)
app.use('/api', router) // 添加前缀
// app.use('/video', routerVideo) // 添加前缀app.listen(PORT, () => {console.log(`Server is running at http://localhost:${PORT}`)
})
3、错误处理中间件
// 错误处理中间件
app.use((err, req, res, next) => {console.log(err)res.status(500).send('service Error')
})
4、 内置中间件
内置中间件
// app.use(express.urlencoded())
app.use(express.json())
5、第三方中间件
第三方中间件