当前位置:网站首页>Express②(路由)
Express②(路由)
2022-04-23 13:37:00 【十八岁讨厌编程】
Express路由
什么是路由
广义上来讲,路由就是映射关系。
现实生活中的路由:
在这里,路由是按键与服务之间的映射关系
在Express中路由指的是客户端的请求与服务器处理函数之间的映射关系。
Express 中的路由分 3 部分组成,分别是请求的类型
、请求的 URL 地址
、处理函数
,格式如下:
路由的匹配过程
每当一个请求到达服务器之后,需要先经过路由的匹配,只有匹配成功之后,才会调用对应的处理函数。
在匹配时,会按照路由的顺序进行匹配,如果请求类型和请求的 URL 同时匹配成功,则 Express 会将这次请求,转交给对应的 function 函数进行处理。
路由匹配的注意点:
- 按照定义的先后顺序进行匹配
- 请求类型和请求的URL同时匹配成功,才会调用对应的处理函数
最简单的路由使用
在 Express 中使用路由最简单的方式,就是把路由挂载到 app 上,示例代码如下:
const express = require('express')
//创建Web服务器,命名为app
const app = express()
// 挂载路由
app.get('/', (req, res) => {
res.send('hello world.')
})
app.post('/', (req, res) => {
res.send('Post Request.')
})
//启动Web服务器
app.listen(80, () => {
console.log('http://127.0.0.1')
})
模块化路由
为了方便对路由进行模块化的管理,Express 不建议将路由直接挂载到 app 上,而是推荐将路由抽离为单独的模块。将路由抽离为单独模块的步骤如下:
① 创建路由模块对应的 .js 文件
② 调用 express.Router() 函数创建路由对象
③ 向路由对象上挂载具体的路由
④ 使用 module.exports 向外共享路由对象
⑤ 使用 app.use() 函数注册路由模块
例如:
创建路由模块:
// 这是路由模块
// 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
注册路由模块:
const express = require('express')
const app = express()
// 1. 导入路由模块
const router = require('./03.router')
// 2. 注册路由模块
app.use(router)
app.listen(80, () => {
console.log('http://127.0.0.1')
})
注意: app.use() 函数的作用,就是来注册全局中间件
为路由模块添加前缀
类似于托管静态资源时,为静态资源统一挂载访问前缀一样,路由模块添加前缀的方式也非常简单:
版权声明
本文为[十八岁讨厌编程]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zyb18507175502/article/details/124352665
边栏推荐
- Get the attribute value difference between two different objects with reflection and annotation
- Unified task distribution scheduling execution framework
- Modification of table fields by Oracle
- Technologie zéro copie
- TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
- Handling of high usage of Oracle undo
- SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
- Oracle database recovery data
- Antd design form verification
- Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
猜你喜欢
Leetcode? The first common node of two linked lists
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Lenovo Savior y9000x 2020
【重心坐标插值、透视矫正插值】原理以及用法见解
Why do you need to learn container technology to engage in cloud native development
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
Explanation of input components in Chapter 16
UML统一建模语言
Tangent space
随机推荐
Logstash数据处理服务的输入插件Input常见类型以及基本使用
Common types and basic usage of input plug-in of logstash data processing service
Influence of openssh version on SSH mutual trust creation in RAC environment
Get the attribute value difference between two different objects with reflection and annotation
Modification of table fields by Oracle
Core concepts of microservice architecture
Solution of discarding evaluate function in surprise Library
MySQL index [data structure + index creation principle]
Analysis of cluster component gpnp failed to start successfully in RAC environment
Tangent space
Lenovo Savior y9000x 2020
PG library checks the name
Storage scheme of video viewing records of users in station B
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions
Apache Atlas Compilation and installation records
JS compares different elements in two arrays
19c RAC steps for modifying VIP and scanip - same network segment
Innobackupex incremental backup
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
Aicoco AI frontier promotion (4.23)