Req.body 已填充,但 Req.body.user 和 Req.body.pass 未填充
Req.body IS Populated, But Req.body.user and Req.body.pass isn't
我正在努力从头开始做 Auth。当 运行 向我的 api 发出 post 请求时,我遇到了 运行 问题。如果我使用 postman 并填充表单数据部分,并且 post,req.body.user 和 req.body.pass 中没有任何内容。但是,当然,req.body 已填充,我可以看到我发送的表单数据。这是我的代码:
const express = require("express"),
app = express(),
shajs = require("sha.js"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/auth', {useNewUrlParser: true});
app.use(bodyParser.urlencoded({ extended: true }));
let userSchema = new Schema({
username: String,
password: String
});
let user = mongoose.model("user",userSchema);
app.post("/user/register",(req,res)=>{
console.log(req.body);
let hashedPass = shajs("sha256").update(req.body.pass).digest("hex");
let newUser = new user({
username: req.body.user,
password: hashedPass
});
});
app.listen(3000,(err,suc)=>{
if(err){
console.log(err);
} else {
console.log("Yay! The app is up on localhost:3000.");
}
});
这是我的 req.body 的样子:
{ '------WebKitFormBoundaryBZtUrMPzNKlsGLwj\r\nContent-Disposition: form-data; name':
'"user"\r\n\r\nBlakskyben\r\n------WebKitFormBoundaryBZtUrMPzNKlsGLwj\r\nContent-Disposition: form-data; name="pass"\r\n\r\nBENNNN\r\n------WebKitFormBoundaryBZtUrMPzNKlsGLwj--\r\n' }
谢谢,本!
bodyParser
不处理 form-data
。为此,您需要使用 multer。
否则使用 application/x-www-form-urlencoded
,因为您已经拥有:
app.use(bodyParser.urlencoded({ extended: true }));
在 Postman 中,不要使用 form-data
无线电,而是使用 x-www-form-urlencoded
无线电。
我正在努力从头开始做 Auth。当 运行 向我的 api 发出 post 请求时,我遇到了 运行 问题。如果我使用 postman 并填充表单数据部分,并且 post,req.body.user 和 req.body.pass 中没有任何内容。但是,当然,req.body 已填充,我可以看到我发送的表单数据。这是我的代码:
const express = require("express"),
app = express(),
shajs = require("sha.js"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/auth', {useNewUrlParser: true});
app.use(bodyParser.urlencoded({ extended: true }));
let userSchema = new Schema({
username: String,
password: String
});
let user = mongoose.model("user",userSchema);
app.post("/user/register",(req,res)=>{
console.log(req.body);
let hashedPass = shajs("sha256").update(req.body.pass).digest("hex");
let newUser = new user({
username: req.body.user,
password: hashedPass
});
});
app.listen(3000,(err,suc)=>{
if(err){
console.log(err);
} else {
console.log("Yay! The app is up on localhost:3000.");
}
});
这是我的 req.body 的样子:
{ '------WebKitFormBoundaryBZtUrMPzNKlsGLwj\r\nContent-Disposition: form-data; name':
'"user"\r\n\r\nBlakskyben\r\n------WebKitFormBoundaryBZtUrMPzNKlsGLwj\r\nContent-Disposition: form-data; name="pass"\r\n\r\nBENNNN\r\n------WebKitFormBoundaryBZtUrMPzNKlsGLwj--\r\n' }
谢谢,本!
bodyParser
不处理 form-data
。为此,您需要使用 multer。
否则使用 application/x-www-form-urlencoded
,因为您已经拥有:
app.use(bodyParser.urlencoded({ extended: true }));
在 Postman 中,不要使用 form-data
无线电,而是使用 x-www-form-urlencoded
无线电。