Node express console.log(req.body)在发布请求中为空

问题描述 投票:0回答:1

快递,我正在尝试建立注册表。为了进行测试,我想将数据从表单字段显示到控制台。但是我在req.body参数中没有得到任何数据。以下是我的server.js文件代码。我不知道我哪里错了

result

您可以在图像中看到花括号为空

Server.js

const express = require('express');
const app = express();
//bringing pool object from dbConfig file
const { pool } = require('./dbConfig');


const port = process.env.PORT || 3000;

/*importing ejs middleware to tell app to render 
ejs files according to the routes defined below*/
app.set('view engine', 'ejs');

//middleware for sending details from frontend to the server {body-parser}
app.use(express.urlencoded({ extended: false }));

//register post request
app.post('/users/register', (req, res) => {
console.log(req.body);
res.send('heelo');

});
node.js postgresql express post
1个回答
0
投票

尝试添加body-parser,并检查您的要求,也许是空的。

const express = require("express");
const app = express();
//bringing pool object from dbConfig file
// const { pool } = require("./dbConfig");
var bodyParser = require("body-parser");

const port = process.env.PORT || 3000;

/*importing ejs middleware to tell app to render 
ejs files according to the routes defined below*/
app.set("view engine", "ejs");

//middleware for sending details from frontend to the server {body-parser}
app.use(express.urlencoded({ extended: false }));

app.use(bodyParser.json());

//register post request
app.post("/users/register", (req, res) => {
  console.log(req.body);
  res.send("heelo");
});

app.listen(port, (err) => {
  if (err) console.error(err);
  else console.log(`server running on port ${port}`);
});
© www.soinside.com 2019 - 2024. All rights reserved.