当前位置:网站首页>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
边栏推荐
- 【多线程】线程通信调度、等待集 wait() 、notify()
- 阿里云刘珅孜:云游戏带来的启发——端上创新
- 2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
- [multithreading] thread communication scheduling, waiting set wait(), notify()
- d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
- Ping An technology's practice of migrating from Oracle to ubisql
- Source NAT address translation and server mapping web page configuration of firewall Foundation
- LeetCode每日一练(主要元素)
- 面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
- 正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
猜你喜欢

当发布/订阅模式遇上.NET

【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)

About MySQL: the phenomenon and background of the problem

What is RPC

Oracle concept 3

Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration

数据中心表格报表实现定制统计加班请假汇总记录分享

Mobile terminal click penetration
P.A.R.A 方法在思源的简易应用(亲测好用)

Open source 23 things shardingsphere and database mesh have to say
随机推荐
ORM表关系及操作
Simulated process scheduling
10 minutes to master the installation steps of MySQL
d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
Unity 阴影——阴影平坠(Shadow pancaking)
List转Table
C语言教师工作量管理系统
C language teacher workload management system
3.4 fixed number of cycles II
Deeply digitise, lead cloud nativity and serve more developers
Source NAT address translation and server mapping web page configuration of firewall Foundation
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
EMQ 助力青岛研博建设智慧水务平台
About MySQL: the phenomenon and background of the problem
基于 Nebula Graph 构建百亿关系知识图谱实践
The array of C language is a parameter to pass a pointer
关于#mysql#的问题:问题遇到的现象和发生背景
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
Sliding window + monotone queue concept and example (p1886 Logu)