为什么 req.body return 在 POST 请求的控制台中是一个空对象?
Why does req.body return an empty object in console in POST request?
在我下面的代码中,中间件应该 return 控制台中的解析数据作为 req.body 访问的对象,但由于某种原因它 return 是一个空对象.
Code and Console Snapshot1
Code and Console Snapshot2
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const { use } = require('express/lib/application');
/*
ulrEncodedParser middleware is used to invoke below function
to parse posted data to POST functions
var urlEncodedParser = bodyParser.urlencoded({extended : false});
var jsonParser = bodyParser.json();
*/
//set view engine to be able to visit views
app.set('view engine', 'ejs');
//middleware for styles to be loaded on pages when req made by views
app.use('/stylesheets', express.static('stylesheets'));
// middleware to parse application/x-www-form-urlencoded
//app.use(bodyParser.urlencoded({ extended: false }));
// middleware to parse application/json
//app.use(bodyParser.json());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//GET "/" req, fires up home page
app.get('/', function(req, res){
res.render('home');
});
//GET "/home" req, aslo fires up home page
app.get('/home', function(req, res){
res.render('home');
});
//GET "/signup" req, fires up sign up page
app.get('/signup', function(req, res){
res.render('signup');
});
//POST information enetered on sign up form
app.post('/signup', function(req, res){
console.log(req.body);
//res.render('signup-success', req.body);
});
//server to run on port 3000
app.listen(3000, function(){
console.log('server listening on port 3000');
});
我还尝试初始化两个单独的变量,其中 urlencoded() 和 bodyParser.json() 的函数可以访问两个中间件的 var 而不是使用 app.use() 中间件这种方式:
var urlEncodedParser = bodyParser.urlencoded({extended : false});
var jsonParser = bodyParser.json();
然后在路由到注册页面的 app.post() 请求中传入每个,但它仍然 return 相同,为空。
这是我的 signup.ejs 也供参考。
<!DOCTYPE html>
<html>
<head>
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" />
<title>Sign Up Page</title>
</head>
<body>
<h1>Sign Up</h1>
<h3>Enter the requested information below for your user profile to be created.</h3><br>
<form id="signup-form" action="/signup" method="post">
<label for="firstName">First Name</label><br>
<input type="text" id="firstName" placeholder="first name"><br><br>
<label for="musicPreferences">Music Prefrences</label><br>
<input type="text" id="musicPrefrence" placeholder="music preferences"><br><br>
<label for="favoriteArtists">Favorite Artists</label<br>
<input type="text" id="favoriteArtists" placeholder="favorite artists"><br><br>
<label for="pastConcerts">Past Concerts Attended</label><br>
<input type="text" id="pastConcerts" placeholder="concerts attended"><br><br>
<label for="futureConcerts">Fututre Concerts Want to Attend</label><br>
<input type="text" id="futureConcerts" placeholder="future concerts"><br><br>
<input type="submit" value="submit"><br>
<%- include('partials/nav.ejs') %>
</form>
</body>
</html>
Sign up Page UI
感谢任何帮助!
后端工作正常,因为当使用 curl 发送请求时,正文显示在控制台中。客户端似乎发送了错误的请求。
screenshot
在我下面的代码中,中间件应该 return 控制台中的解析数据作为 req.body 访问的对象,但由于某种原因它 return 是一个空对象.
Code and Console Snapshot1 Code and Console Snapshot2
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const { use } = require('express/lib/application');
/*
ulrEncodedParser middleware is used to invoke below function
to parse posted data to POST functions
var urlEncodedParser = bodyParser.urlencoded({extended : false});
var jsonParser = bodyParser.json();
*/
//set view engine to be able to visit views
app.set('view engine', 'ejs');
//middleware for styles to be loaded on pages when req made by views
app.use('/stylesheets', express.static('stylesheets'));
// middleware to parse application/x-www-form-urlencoded
//app.use(bodyParser.urlencoded({ extended: false }));
// middleware to parse application/json
//app.use(bodyParser.json());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//GET "/" req, fires up home page
app.get('/', function(req, res){
res.render('home');
});
//GET "/home" req, aslo fires up home page
app.get('/home', function(req, res){
res.render('home');
});
//GET "/signup" req, fires up sign up page
app.get('/signup', function(req, res){
res.render('signup');
});
//POST information enetered on sign up form
app.post('/signup', function(req, res){
console.log(req.body);
//res.render('signup-success', req.body);
});
//server to run on port 3000
app.listen(3000, function(){
console.log('server listening on port 3000');
});
我还尝试初始化两个单独的变量,其中 urlencoded() 和 bodyParser.json() 的函数可以访问两个中间件的 var 而不是使用 app.use() 中间件这种方式:
var urlEncodedParser = bodyParser.urlencoded({extended : false});
var jsonParser = bodyParser.json();
然后在路由到注册页面的 app.post() 请求中传入每个,但它仍然 return 相同,为空。
这是我的 signup.ejs 也供参考。
<!DOCTYPE html>
<html>
<head>
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css" />
<title>Sign Up Page</title>
</head>
<body>
<h1>Sign Up</h1>
<h3>Enter the requested information below for your user profile to be created.</h3><br>
<form id="signup-form" action="/signup" method="post">
<label for="firstName">First Name</label><br>
<input type="text" id="firstName" placeholder="first name"><br><br>
<label for="musicPreferences">Music Prefrences</label><br>
<input type="text" id="musicPrefrence" placeholder="music preferences"><br><br>
<label for="favoriteArtists">Favorite Artists</label<br>
<input type="text" id="favoriteArtists" placeholder="favorite artists"><br><br>
<label for="pastConcerts">Past Concerts Attended</label><br>
<input type="text" id="pastConcerts" placeholder="concerts attended"><br><br>
<label for="futureConcerts">Fututre Concerts Want to Attend</label><br>
<input type="text" id="futureConcerts" placeholder="future concerts"><br><br>
<input type="submit" value="submit"><br>
<%- include('partials/nav.ejs') %>
</form>
</body>
</html>
Sign up Page UI
感谢任何帮助!
后端工作正常,因为当使用 curl 发送请求时,正文显示在控制台中。客户端似乎发送了错误的请求。
screenshot