Parameter acquisition process -Post 20-Node.js study notes request -Express-

Post acquisition parameters

Express post request parameters received packets need to use a third-party body-parser

//引入body-parser模块
const bodyParser  = require('body-parser');
//配置body-parser模块
app.use(bodyParser.urlencoded({ extended:false}));
//接收请求
app.post('/add',(req,res)=>{
    //接收请求参数
    console.log(req.body)
})
  • extended: false: using a system module represents the querystring to deal with, but also the official recommendation
  • extended: true: indicate the use of third-party modules to handle qs
  • From functional terms, qs to be more powerful than querystring, so here can be considered based on the actual needs of the project
//10.js
//引入express框架
const express  = require('express');
const bodyParser = require('body-parser');
//创建网站服务器
const app = express();

//拦截所有的请求
app.use(bodyParser.urlencoded({extended:false}));
app.post('/add',(req,res)=>{
    //获取请求参数
    res.send(req.body)
})

//监听端口
app.listen(3000);
console.log('网站服务器启动成功');
//post.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
   <form action="http://localhost:3000/add" method="post">
        <input type="text" name="uname">
        <input type="password" name="password">
        <input type="submit">
   </form>
</body>
</html>

Guess you like

Origin www.cnblogs.com/foreverLuckyStar/p/12089491.html
Recommended