当前位置:网站首页>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);
});

边栏推荐
- asdsadadsad
- Rew acoustic test (II): offline test
- Bind threads to run on a specific CPU logical kernel
- Mmdet line by line code interpretation of positive and negative sample sampler
- [cmake] make command cannot be executed normally
- Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
- Generate directory in markdown
- 维基媒体基金会公布新商业产品“维基媒体企业”首批客户
- Baidu map JS browsing terminal
- Using appbarlayout to realize secondary ceiling function
猜你喜欢

Deep understanding of kotlin collaboration context coroutinecontext

ES6 learning path (II) let & const

Find the number that appears only once in the array

Baidu map JS browsing terminal

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

Detailed explanation of pipline of mmdetection

Qt连接神通数据库

Anchorgenerator for mmdet line by line interpretation
![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)
[data analysis and display]

Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
随机推荐
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Talking about the difference between kotlin collaboration and thread
Flink Exception -- No ExecutorFactory found to execute the application
Common query and aggregation of ES
Interviewer: do you understand the principle of recyclerview layout animation?
7. know JNI and NDK
Occasionally, Flink data is overstocked, resulting in checkpoint failure
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
C # get the current timestamp
Talk about how the kotlin process started?
Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
Is it safe to open an account? How can anyone say that it is not reliable.
Abstract factory pattern
Find the number that appears only once in the array
QT downloading files through URL
Rew acoustic test (I): microphone calibration
C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
Mmdet line by line code interpretation of positive and negative sample sampler
ES6 learning road 5 symbol