【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路由与响应方法

正在更新。。。

相关推荐
Bolt14 小时前
用 pnpm 11 省掉项目里的 .nvmrc 与 .npmrc
前端·npm·node.js
学习使我快乐0120 小时前
Express 学习
学习·node.js·express
IT策士21 小时前
Python 中间件系列:文件存储minio操作操
开发语言·python·中间件
驾驭人生1 天前
企业级微服务基础设施 | Docker Compose 9 大中间件 本地私有仓库 一键部署脚本前言
docker·微服务·中间件
Zeluar1 天前
Node.js安装显示旧版本存在且无法覆盖
node.js
IT策士1 天前
Python 中间件系列:消息队列 RabbitMQ 操作
python·中间件·rabbitmq
孟陬1 天前
Node.js v26.0 新增超甜的语法糖 getOrInsert / getOrInsertComputed 介绍
python·node.js
淼_@淼1 天前
node.js、node、nvm、npm、npx的关系
node.js
小粉粉hhh1 天前
Node.js(四)——npm与包
前端·npm·node.js
祁_z1 天前
Pydantic 数据校验 & 限流中间件(限制每个 IP 的请求频率,防止接口被刷爆)
网络协议·tcp/ip·中间件