当前位置:网站首页>07. Express routing
07. Express routing
2022-06-27 16:54:00 【Take charge of [email protected]】
1、 What is routing
【 explain 】: Broadly speaking , Routing is a mapping relationship .
【 Explanation of life cases 】:
2、Express The routing
【 explain 】: Routing refers to the mapping between client requests and server processing functions .
【 Grammar format 】:
app.METHOD(PATH ,HANDLER)
【 Case study 】:
const express = require('express')
const app = express()
// Mount route
app.get('/', (req, res) => {
res.send('hello world!!')
})
app.post('/', (req, res) => {
res.send('Post Request')
})
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
3、 Route matching process
【 explain 】: 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 .
【 The illustration 】:
【 matters needing attention 】:
- Match in defined order
- The type of request and the type of request URL Simultaneous matching , Will call the corresponding processing function
4、 Use of routes
1、 Basic use
【 explain 】: stay Express The easiest way to use routing in , Is to mount the route to app On
【 Code example 】:
const express = require('express')
const app = express()
// Mount route
app.get('/', (req, res) => {
res.send('hello world!!')
})
app.post('/', (req, res) => {
res.send('Post Request')
})
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
2、 Modular routing
【 Creating steps 】:
- Create the corresponding route js file
- call express.Router() Function to create a routing object
- Mount the specific route to the routing object
- Use moudle.exports Share routes outward
- Use app.use() Function register route
【 Code case 】:
- route user.js file
//1、 Import express
const express = require('express')
//2、 Create routing objects
const router = express.Router()
//3、 Define specific routes
router.get('/user', (req,res)=>{
res.send('Get User List!!!')
})
router.post('/User/add', (req,res)=>{
res.send('Add new user!!!')
})
//4、 Export route
module.exports=router
- Register and use routing
const userRouter = require('./user.js')
// Use app.use() Register routing module
app.use(userRouter)
3、 Add prefix to the routing module
【 demonstration 】:
const userRouter = require('./user.js')
// Use app.use() Register routing module
app.use('/api', userRouter)
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202161538073400.html
边栏推荐
- Leetcode daily practice (main elements)
- Realize simple three-D cube automatic rotation
- 泰山OFFICE技术讲座:第一难点是竖向定位
- Leetcode daily practice (Yanghui triangle)
- 数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
- Detailed explanation of transaction isolation level
- Relation and operation of ORM table
- C语言集合运算
- C language teacher workload management system
- EMQ 助力青岛研博建设智慧水务平台
猜你喜欢

Oracle概念二

After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines

Redis Series 2: data persistence improves availability

Julia constructs diagonal matrix

Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le

Simulated process scheduling

【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。

When the publish / subscribe mode encounters NET

Sliding window + monotone queue concept and example (p1886 Logu)

Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
随机推荐
树莓派初步使用
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
Oracle概念三
华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
Mihayou sued Minmetals trust, which was exposed to product thunderstorms
C语言教师工作量管理系统
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
List to table
EMQ 助力青岛研博建设智慧水务平台
About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
C language course design
New method of cross domain image measurement style relevance: paper interpretation and code practice
P. Simple application of a.r.a method in Siyuan (friendly testing)
tensorflow求解泊松方程
Cesium realizes satellite orbit detour
Annual comprehensive analysis of China's audio market in 2022
Raspberry pie preliminary use
Deeply digitise, lead cloud nativity and serve more developers