Node.js使用express时获取表单post请求体设置

Node.js使用express时获取表单post请求体设置

在node.js使用express中没有直接的POST请求体,需要另外安装配置第三方包进行使用

  1. 下载安装包
    npm install --save body-parser

  2. 导包
    Node.js使用express时获取表单post请求体设置

  3. 配置
    代码
    app.use(bodyParser.urlencoded({extended:false})); app.use(bodyParser.json());

  4. 使用
    代码app.post('/post',function(req,res){ var comment = req.body; comment.dateTime = '2020-06-12'; comments.unshift(comment); res.redirect('/'); });
    Node.js使用express时获取表单post请求体设置