req.body获取不到post数据?
发布于 8 年前 作者 songqinghehe 12832 次浏览 来自 问答

express -V === 4.13.4 app.js code: var express = require(“express”);
var bodyParser = require(“body-parser”);
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.post(’/login’,function(req,res){
var user_name=req.body.user;
var password=req.body.password;
console.log("User name = “+user_name+”, password is "+password);
res.end(“yes”);
});

app.listen(3000);

postman请求返回: User name = undefined, password is undefined

请问大神这是怎么回事?

3 回复

打印req.body  如果里面没有说明提交未成功

@TimLiu1 谢谢回答,刚搜了一下问题,是由于我用postman 的form-data提交的,需要一个中间件来支持

记录一下获取postman的form-data数据的中间件 var express = require(‘express’); var bodyParser = require(‘body-parser’); var multer = require(‘multer’); var app = express();

//for parsing application/json app.use(bodyParser.json());

//for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true }));

//for parsing multipart/form-data //需要用npm install multer@0.1.8 app.use(multer());

回到顶部