【Node.js工程师养成计划】之express中间件与接口规范

一、Express中间件的概念与基本应用

javascript 复制代码
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}`)
})

封装个方法:

javascript 复制代码
const express = require('express')

// 加一个注释,用以说明,本项目代码可以任意定制更改
const app = express()

const PORT = process.env.PORT || 3000

function 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}`)
})

发现也并不完美

写个中间件

javascript 复制代码
const express = require('express')

// 加一个注释,用以说明,本项目代码可以任意定制更改
const app = express()

const PORT = process.env.PORT || 3000

app.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、应用程序级别中间件

javascript 复制代码
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

javascript 复制代码
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

javascript 复制代码
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、错误处理中间件

javascript 复制代码
// 错误处理中间件
app.use((err, req, res, next) => {
  console.log(err)
  res.status(500).send('service Error')
})

4、 内置中间件

内置中间件

javascript 复制代码
// app.use(express.urlencoded())
app.use(express.json())

5、第三方中间件

第三方中间件

三、Express路由与响应方法

正在更新。。。

相关推荐
katasea9 小时前
第05章:信创技术栈选型:服务器、操作系统、数据库、中间件适配对比
服务器·数据库·中间件
Wx-bishekaifayuan21 小时前
django个性化旅游路线推荐平台49005-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
liangshanbo12151 天前
面试题:如何优化 Webpack 的打包速度?
前端·webpack·node.js
Wx-bishekaifayuan1 天前
springboot生活易购超市管理系统54086-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
2601_962177301 天前
Windows 安装 Codex CLI 入门级教程
人工智能·windows·node.js·ai编程
yexianglunbai1 天前
中间件(Middleware)详解:概念、作用与实战
java·开发语言·中间件
光影少年1 天前
RN与Flutter架构区别
前端·flutter·react native·react.js·架构·node.js
染指11102 天前
128.Agent-LangChain核心组件-中间件-调用模型的时候
人工智能·windows·中间件·langchain·agent
梅孔立2 天前
Codex 完整教程(Node.js安装 + npm安装Codex + DeepSeek配置 + 快捷键 + 设置)
前端·npm·node.js
染指11102 天前
129.Agent-LangChain核心组件-自定义中间件-通过类实现(AgentMiddleware)
人工智能·中间件·langchain·agent·agents