Express中间件


音响暴躁
音响暴躁 2022-09-21 09:15:04 49398
分类专栏: 资讯

一、中间件的格式

  中间件函数的形参列表中,必须包含next参数。而路由处理函数中只包含req和res

const express = require('express')
const app = express()
app.get('/',(req,res,next()=>{
	next()
})
app.listen(80,()=>{})
s="token punctuation">,res,next){
    console.log('第二个全局中间件');
    next();
})
// 定义一个路由
app.get('/user', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
// 多个局部生效的中间件
const express = require('express')
const app = express()

// 1.定义中间件函数
const mw1 = (req,res,next)=>{
    console.log('调用了第一个局部生效的中间件');
    next()
}
const mw2 = (req,res,next)=>{
    console.log('调用了第二个局部生效的中间件');
    next()
}

// 2。创建路由
app.get('/',mw1,mw2,(req,res)=>{
    res.send('111')
})
app.get('/user',(req,res)=>{
    res.send('2222')
})
app.listen(80,()=>{
    console.log('http:127.0.0.1');
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2、路由级别的中间件
  绑定到express.Router()实例上的中间件,叫做路由级别的中间件。应用级别的中间件是绑定到app实例上,路由级别中间件绑定到router实例上

// 这是路由模块
// 1.导入express
const express = require('express')
// 2.创建路由对象
const router = express.Router()

// 3.挂载具体的路由
router.get('/user/list',(req,res)=>{
    res.send('Get user list.')
})
router.post('/user/add',(req,res)=>{
    res.send('Add new user.')
})
// 4.向外导出路由对象
module.exports = router
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3、错误级别的中间件

  • 作用:专门用来捕获整个项目中发生的异常错误,从而防止项目异常崩溃的问题
  • 格式:错误级别中间件的function处理函数中,必须有4个形参,形参顺序从前到后,分别是(err,req,res,next)
  • 注意:必须路由在前,中间件在后
const express = require('express')
const app = express()
const port = 80

// 1.定义路由
app.get('/',(req,res)=>{
    // 1.1人为制造错误
    throw new Error('服务器内部发生了错误!')
    res.send('Home page.')
})
// 2.定义错误级别的中间件,捕获整个项目中的异常错误,从而防止程序的崩溃
app.use((err,req,res,next)=>{
    console.log('发生了错误! '+err.message);
    res.send('Error :'+err.message)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4、Express内置的中间件

  • express.static 快速托管静态资源的内置中间件,例如:html文件,图片,CSS样式等(无兼容性)
  • express.json 解析json格式的请求体数据(有兼容性)
  • express.urlencoded 解析URL-encoded格式的请求提数据(有兼容性)
// 导入express模块
const express = require('express')
// 创建express的服务实例
const app= express()

// 注意:出了错误级别的中间件,其他的中间件,必须在路由之前进行配置
// 通过express.json()这个中间件,解析表单中的JSON格式的数据
app.use(express.json())
// 通过express.urlencoded()这个中间件,来解析表单中的url-encoded格式的数据
app.use(express.urlencoded({extended:false}))

app.post('/user',(req,res)=>{
    //在服务器,可以使用req.body这个属性,来接收客户端发过来的请求体数据
    //默认情况下,如果不配置解析表单数据的中间件,则req.body默认等于undefined
    console.log(req.body);
    res.send('ok')
})

app.post('/book',(req,res)=>{
    // 在服务器端,可以通过req.body来获取JSON格式的表单数据和url-encoded格式的数据
    console.log(req.body);
    res.send('okk')
})
// 调用app.listen方法,指定端口号并启动服务器
app.listen(80,(req,res)=>{
    console.log('http://127.0.0.1');
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5、第三方的中间件
实现步骤:

  • 运行npm install body-parser安装中间件
  • 使用require导入中间件
  • 调用app.use()注册并使用中间件
// 导入express模块
const express = require('express')
// 创建express的服务实例
const app= express()

// 1.导入解析表单数据的中间件body-parser
const parser = require('body-parser')
// 2.使用app.use()注册中间件
app.use(parser.urlencoded({extended:false}))

app.post('/user',(req,res)=>{
    console.log(req.body);
    res.send('ok')
})

// 调用app.listen方法,指定端口号并启动服务器
app.listen(80,(req,res)=>{
    console.log('http://127.0.0.1');
})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

6、自定义中间件
实现步骤:

  • 定义中间件
  • 监听req的data事件
  • 监听req的end事件
  • 使用querystring模块解析请求体数据
    • Node.js内置了一个querystring模块,专门用来处理查询字符串。通过这个模块提供的parse()函数,可以轻松把查询字符串解析成对象的格式
      在这里插入图片描述
  • 将自定义中间件封为模块
    在这里插入图片描述
    在这里插入图片描述

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=8712
赞同 0
评论 0 条
音响暴躁L1
粉丝 0 发表 12 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2969
【软件正版化】软件正版化工作要点  2888
统信UOS试玩黑神话:悟空  2860
信刻光盘安全隔离与信息交换系统  2746
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1280
grub引导程序无法找到指定设备和分区  1249
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  169
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  168
点击报名 | 京东2025校招进校行程预告  165
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  161
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!