当前位置:网站首页>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
边栏推荐
- Open source 23 things shardingsphere and database mesh have to say
- Kubernetes基础自学系列 | Ingress API讲解
- P. Simple application of a.r.a method in Siyuan (friendly testing)
- 泰山OFFICE技术讲座:第一难点是竖向定位
- IDE Eval reset unlimited trial reset
- Extract field year / quarter effect based on date
- Under the influence of external factors, how to manage internal resources and reduce costs is the key
- [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
- Ping An technology's practice of migrating from Oracle to ubisql
- 国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
猜你喜欢
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
关于#mysql#的问题:问题遇到的现象和发生背景
3.1 simple condition judgment
3.4 fixed number of cycles II
[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
模拟进程调度
[fxcg] today's market analysis
[multithreading] thread communication scheduling, waiting set wait(), notify()
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
Mobile terminal click penetration
随机推荐
防火墙基础之源NAT地址转换和服务器映射web页面配置
What is the level 3 password complexity of ISO? How often is it replaced?
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
Data center table reports realize customized statistics, overtime leave summary record sharing
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
Pragma once Usage Summary
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
3.4 fixed number of cycles II
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
3.2 multiple condition judgment
ORM表关系及操作
The European unified charging specification act was passed before the end of the year, and it is planned to expand to products such as laptop and keyboard
Leetcode daily practice (main elements)
事务的四大特性
Practice of constructing ten billion relationship knowledge map based on Nebula graph
10分钟掌握mysql的安装步骤
【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)