当前位置:网站首页>Express ② (routing)
Express ② (routing)
2022-04-23 13:47:00 【Eighteen hates programming】
List of articles
Express route
What is routing
Broadly speaking , Routing is a mapping relationship .
Real life routing :

ad locum , Routing is the mapping relationship between keys and services
stay Express The middle path refers to the mapping relationship between the client's request and the server's processing function .
Express The routing in is divided into 3 Part of it is made up of , Namely Type of request 、 Requested URL Address 、 Processing function , The format is as follows :

Routing matching process
Every time a request arrives at the server , A match that needs to be routed first , Only after the match is successful , Will call the corresponding processing function .
In the match , Will match in the order of routing , If the request type and the requested URL At the same time, the match is successful , be Express Will send this request , Forward to the corresponding function Function to process .

Considerations for route matching :
- Match in defined order
- The type of request and the type of request URL At the same time, the match is successful , Will call the corresponding processing function
Simplest route use
stay Express The easiest way to use routing in , Is to mount the route to app On , The sample code is as follows :
const express = require('express')
// establish Web The server , Name it app
const app = express()
// Mount route
app.get('/', (req, res) => {
res.send('hello world.')
})
app.post('/', (req, res) => {
res.send('Post Request.')
})
// start-up Web The server
app.listen(80, () => {
console.log('http://127.0.0.1')
})
Modular routing
In order to facilitate the modular management of routing ,Express It is not recommended to mount the route directly to app On , Instead, it's recommended to pull the route out into a separate module . The steps to extract a route into a separate module are as follows :
① Create the routing module corresponding to .js file
② call express.Router() Function to create a routing object
③ Mount the specific route to the routing object
④ Use module.exports Share routing objects out
⑤ Use app.use() Function registration routing module
for example :
Create routing module :
// This is the routing module
// 1. Import express
const express = require('express')
// 2. Create routing objects
const router = express.Router()
// 3. Mount specific routing
router.get('/user/list', (req, res) => {
res.send('Get user list.')
})
router.post('/user/add', (req, res) => {
res.send('Add new user.')
})
// 4. Export routing objects out
module.exports = router
Register routing module :
const express = require('express')
const app = express()
// 1. Import routing module
const router = require('./03.router')
// 2. Register routing module
app.use(router)
app.listen(80, () => {
console.log('http://127.0.0.1')
})
Be careful : app.use() Function function , Is to register the global middleware
Prefix routing module
Similar to when hosting static resources , Uniformly mount access prefixes for static resources , The way the routing module adds a prefix is also very simple :

版权声明
本文为[Eighteen hates programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231337076644.html
边栏推荐
猜你喜欢

PG SQL intercepts the string to the specified character position

零拷贝技术

Leetcode brush question 897 incremental sequential search tree

Express②(路由)

Logstash数据处理服务的输入插件Input常见类型以及基本使用

Detailed explanation of constraints of Oracle table

Campus takeout system - "nongzhibang" wechat native cloud development applet

Exemple de méthode de réalisation de l'action d'usinage à point fixe basée sur l'interruption de déclenchement du compteur à grande vitesse ob40 pendant le voyage de tia Expo
![MySQL [SQL performance analysis + SQL tuning]](/img/71/2ca1a5799a2c7a822158d8b73bd539.png)
MySQL [SQL performance analysis + SQL tuning]

联想拯救者Y9000X 2020
随机推荐
函数只执行第一次的执行一次 once函数
SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
Operations related to Oracle partition
Lenovo Savior y9000x 2020
Test the time required for Oracle library to create an index with 7 million data in a common way
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
Why do you need to learn container technology to engage in cloud native development
Error 403 in most cases, you or one of your dependencies are requesting
Leetcode brush question 897 incremental sequential search tree
Special window function rank, deny_ rank, row_ number
MySQL index [data structure + index creation principle]
Solution of discarding evaluate function in surprise Library
ACFs file system creation, expansion, reduction and other configuration steps
Troubleshooting of expdp export error when Oracle table has logical bad blocks
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
Logstash数据处理服务的输入插件Input常见类型以及基本使用
Utilisation de GDB
Oracle and MySQL batch query all table names and table name comments under users
Dynamic subset division problem