当前位置:网站首页>06. First introduction to express
06. First introduction to express
2022-06-27 16:54:00 【Take charge of [email protected]】
1、 What is? Express
【 explain 】: Express Is based on Node.js platform , Fast 、 to open up 、 minimalist Web Development framework .
【 The essence 】: It's just one. npm Third party packages on , Provides a quick way to create Web A convenient way for servers
【 Chinese official website 】: https://www.expressjs.com.cn/
2、Express The role of
- For front-end programmers , The two most common servers , Namely :
- Web Web server : Specialized in providing Web Web resource server .
- API Interface server : Specialized in providing API Interface server .
3、Express Basic use of
1、 install Express
In the directory where the project is located , Run the following terminal command , Can be express Install into the project to use :
npm i [email protected]4.17.1
2、 Create basic web The server
// Import express
const express = require('express')
// establish web The server
const app = express();
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
3、 monitor GET request
【 explain 】: adopt app.get Can listen to the user's get request The specific syntax is as follows
// Parameters 1: Represents the address requested by the client
// Parameters 2: Requested handler
// req: Request object
// res: The response object
app.get('url',(req,res)=>{
} /* Processing function */)
【 Code example 】:
// Import express
const express = require('express')
// establish web The server
const app = express();
// Listen to the client get request
app.get('/user', (req, res) => {
// call res.send() Method to respond to a client json object
res.send({
name: ' Zhang San ',
age: 20,
gender: ' male '
})
})
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
4、 monitor POST request
【 explain 】: adopt app.post Can listen to the user's get request The specific syntax is as follows
// Parameters 1: Represents the address requested by the client
// Parameters 2: Requested handler
// req: Request object
// res: The response object
app.post('url',(req,res)=>{
} /* Processing function */)
【 Code example 】:
// Import express
const express = require('express')
// establish web The server
const app = express();
// Listen to the client post request
app.post('/user', (req, res) => {
// call res.send() Respond to a string
res.send(' The request is successful !!')
})
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
5、 The response content is sent to the client
【 explain 】: adopt res.send() Method , You can put the processed content , Send to client :
【 Code example 】:
// Import express
const express = require('express')
// establish web The server
const app = express();
// Listen to the client get request
app.get('/user', (req, res) => {
// call res.send() Method to respond to a client json object
res.send({
name: ' Zhang San ',
age: 20,
gender: ' male '
})
})
app.post('user',(req,res)=>{
res.send(' The request is successful !!!')
})
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
6、 Get query URL Query parameters in
【 explain 】: adopt req.query object , You can access the client in the form of a query string , Parameters sent to the server
【 Code example 】:
// Import express
const express = require('express')
// establish web The server
const app = express();
// Get two url Parameters carried in
// adopt req.query You can get the passed parameters
// By default, it's an empty object
app.get('/', (req, res) => {
// Client side usage ?name=as&age=20 The parameters sent to the server can be sent through req.query Object access to
console.log(req.query);
res.send(req.query)
})
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
7、 obtain URL Dynamic parameters in
【 explain 】: Can pass req.params object , visit url in , adopt : Matching dynamic parameters :
【 Code example 】:
// Import express
const express = require('express')
// establish web The server
const app = express();
// Get dynamic parameters
// Default req.params Is an empty object
app.get('/user/:id',(req,res)=>{
console.log(req.params)
res.send(req.params)
})
// Start the server
app.listen(80, () => {
console.log('express running at http://127.0.0.1');
})
8、 Hosting static resources
1、express.static()
【 explain 】: express Provides a very useful function , be called express.static(), Through it , We can easily create a static resource server .
【 Use the syntax 】:
app.use(express.static('public'))
【 explain 】:
- You can visit public All the files in the directory :
- Express Find the file in the specified static Directory , And provide access path to resources . therefore , The directory name where the static file is stored will not appear in URL in
2、 Hosting multiple static resource directories
【 grammar 】:
app.use(express.static('public'))
app.use(express.static('files'))
【 explain 】:
- Call it many times
- Use the method of sequential search to find
3、 Mount path prefix
【 explain 】: If you want to be ahead of the managed static resource access path , Mount path prefix , You can use the following method :
app.use('/public' ,express.static('public'))
9、nodemon
【 brief introduction 】: Writing debugging Node.js When the project is , If you change the project code , It requires frequent manual close fall , Then restart , Is cumbersome . Now? , We can use nodemon This tool , It can monitor changes in project files , When the code is modified ,nodemon Will automatically help us restart the project , It is very convenient for development and debugging
1、 install
npm i nodemon --save
2、 Use nodemon
nodemon app.js
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202161538073441.html
边栏推荐
- d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
- 鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
- 【多线程】线程通信调度、等待集 wait() 、notify()
- 事务的四大特性
- 2022年中国音频市场年度综合分析
- About MySQL: the phenomenon and background of the problem
- 2/14 preliminary calculation geometry
- 鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
- What do fast fashion brands care more about?
- [Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
猜你喜欢

3.3 one of the fixed number of cycles

Halcon: discrete digital OCR recognition
![[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth](/img/70/fa79ba38e28c41ed28bce2ec73cd79.png)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth

Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie

Autodesk NavisWorks 2022 software installation package download and installation tutorial

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

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

How to improve it electronic equipment performance management

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
随机推荐
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
Oracle concept II
如何提升IT电子设备效能管理
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
特殊函数计算器
Oracle concept 3
Sliding window + monotone queue concept and example (p1886 Logu)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)
C système de gestion de la charge de travail des enseignants en langues
Raspberry pie preliminary use
The array of C language is a parameter to pass a pointer
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
Leetcode daily practice (longest substring without repeated characters)
What is the level 3 password complexity of ISO? How often is it replaced?
关于VS2019C#如何建立登陆界面输入的用户名和密码需与Access数据库的记录相匹配
Bit. Store: long bear market, stable stacking products may become the main theme
Huawei cloud devcloud launched four new capabilities, setting two domestic firsts
What is RPC
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾