当前位置:网站首页>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
边栏推荐
- At the same time, the problems of height collapse and outer margin overlap are solved
- Android 面试主题集合整理
- 软考系统集成项目管理工程师全真模拟题(含答案、解析)
- Apache Atlas Compilation and installation records
- JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
- sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
- Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
- Dolphin scheduler configuring dataX pit records
- Leetcode? The first common node of two linked lists
- Use of GDB
猜你喜欢
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
Apache seatunnel 2.1.0 deployment and stepping on the pit
零拷貝技術
Plato farm, a top-level metauniverse game, has made frequent positive moves recently
SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
校园外卖系统 - 「农职邦」微信原生云开发小程序
面试官给我挖坑:URI中的 “//” 有什么用?
Oracle job scheduled task usage details
Search ideas and cases of large amount of Oracle redo log
随机推荐
Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
Analysis of redo log generated by select command
The interviewer dug a hole for me: what's the use of "/ /" in URI?
SQL learning | set operation
Test on the time required for Oracle to delete data with delete
Move blog to CSDN
Building MySQL environment under Ubuntu & getting to know SQL
Dolphin scheduler source package Src tar. GZ decompression problem
Oracle view related
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
PG SQL intercepts the string to the specified character position
Longitude and latitude position of provincial capitals in China
Technologie zéro copie
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
QT calling external program
Oracle and MySQL batch query all table names and table name comments under users
MySQL and PgSQL time related operations