当前位置:网站首页>Express の post request
Express の post request
2022-06-30 09:16:00 【The man of Jike will never admit defeat】
Express の POST request
Last article yes Express In dealing with GET request . Here we'll see how to express In dealing with POST request .
To move to POST, The most important thing is that GET More requests Request body . The key is how to get POST The body of the request . stay express in , Handle POST Request needs to use post() function .
The type of the request body is determined by Content-Type The request header specifies ,POST Common request body types for requests are
application/x-www-form-urlencoded: The data is encoded as&Separated key value pairs , At the same time=Separate key and value . Characters that are not letters or numbers will be Percentage coding , Also known as URL code .
You can see that the Chinese in the request body is encoded by a percentage . Because of the percentage code , This makes this type of request body unable to send binary data , Therefore, when uploading files , Use only the followingmultipart/form-dataType substitutionmultipart/form-data: If you want to send this type of request body , Required formmethodThe attribute isPOSTAndenctypeThe attribute ismultipart/form-data. Of course , This type of request body can also exclude binary data , It can be just a simple form .
<form action="http://localhost:3000/login" method="post" enctype="multipart/form-data">
<input type="text" name="username" placeholder=" full name "/><br>
<input type="password" name="password" placeholder=" password "/><br>
<input type="file" name="file" id="file"><br>
<button type="submit"> Submit </button>
</form>
application/json: Now the more common type isJSONType , You can usually addcharset=utf-8;Specify the encoding of the request body .
middleware
In order to make express Have the ability to handle POST The ability to ask , We need to use middleware app.use().
app.use(express.urlencoded({
extended: false
}));
app.use(express.json());
express.urlencoded() and express.json() All are express Built-in middleware . Separately for processing application/x-www-form-urlencoded and application/json Request body of type . among {extended: false} Representation resolution URL code When using querystring library , Instead, use qs library .
Get request parameters
Use req.body Get the request body
app.post('/login', (req, res) => {
let {
username, password } = req.body;
console.log('username: ', username);
console.log('password: ', password);
res.json(req.body);
});

边栏推荐
- Abstract factory pattern
- C # get the current timestamp
- Concatapter tutorial
- Rew acoustic test (VI): signal and measurement
- Common query and aggregation of ES
- Talk about the job experience of kotlin cooperation process
- Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
- C accesses mongodb and performs CRUD operations
- Rew acoustic test (IV): test principle of rew
- Deep understanding of continuation principle
猜你喜欢

Esp32 (4): overview of the overall code architecture

Talk about the kotlin cooperation process and the difference between job and supervisorjob

Qt连接神通数据库

Flink SQL custom connector

Pytorch BERT

asdsadadsad

Rew acoustic test (I): microphone calibration

Applet learning path 2 - event binding

Raspberry pie 4B no screen installation system and networking using VNC wireless projection function

Concatapter tutorial
随机推荐
Sort (simple description)
Esp32 (IX): OTA function of function development
What are the SQL add / delete / modify queries?
Dart asynchronous task
Use of Baidu face recognition API
JPA naming rules
Mmcv expanding CUDA operator beginner level chapter
asdsadadsad
Advanced technology management -- how managers design and build echelons
Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
Pit encountered by fastjason
Set, map and modularity
Agp7.0|kts makes a reinforced plug-in
Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
Talking about kotlin process exception handling mechanism
Detailed explanation of pytoch's scatter function
100 lines of code and a voice conversation assistant
Detailed explanation of rect class
Introduction to MySQL basics day4 power node [Lao Du] class notes
[wechat applet] realize applet pull-down refresh and pull-up loading