【问题标题】:Express 4.17 req.body is emptyExpress 4.17 req.body 为空
【发布时间】:2020-12-17 12:34:06
【问题描述】:

我希望你能在这里帮助我,因为在尝试了十几个 s/o 解决方案并阅读了几次快速文档之后,我很难过。我正在构建一个 Node 应用程序,它将(最终)接受来自前端应用程序的 POST,将其保存到 Mongo,然后允许后端用户操作数据。我刚刚开始,试图让 POST 路线工作并到目前为止:

app.js:

const express = require("express");
const cors = require("cors");

const mongoose = require('mongoose');
const AppData = require("./model/AppData");
const uri = "mongodb://localhost:27017/lunch"
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });
const connection = mongoose.connection;

const router = require("./routes/index");
const PORT = 3005;
const app = express();

app.use(cors());
app.use("/", router);
app.use(express.raw({type: "application/json"}));
app.use(express.json({strict: false}));

connection.once('open', () => {
    console.log('????Successfully connected to MongoDB????');
});

app.listen(PORT, function () {
    console.log(`????The Backend Server is up and running on port ${PORT}????`);
});

index.js(路由...计划更改名称)

const express = require('express');
const router = express.Router();
const appDataController = require('../controllers/appDataController');

router.post('/submit', appDataController.createAppData);

module.exports = router;

和 appDataController.js:

const mongoose = require('mongoose');
const AppData = mongoose.model('AppData');

exports.createAppData = (req, res) => {
    let reqData = req.body;
    console.log(reqData);
    res.send(reqData);
}

真的很简单,但是当我抓住 Postman 并使用 body/raw/json 设置请求并发送时

{
    "name": "John",
    "age": "21"
}

我总是看到 body 是未定义的。从我所看到的一切来看,我没有做错任何事,但结果清楚地表明不是……我错过了什么?

【问题讨论】:

  • app.use(express.raw({type: "application/json"})); - 但是为什么呢?
  • 如果我的回答对你有帮助,请务必设置为答案。

标签: node.js json express post postman


【解决方案1】:

您还应该包含urlencoded 选项,以便在x-www-form-urlencoded 正文上获取POST 请求的正文。

const app = express();

app.use(cors()); 
app.use(express.raw({type: "application/json"}));
app.use(express.json({strict: false}));
app.use(express.urlencoded({extended: false})); // include this line
app.use("/", router);

【讨论】:

    【解决方案2】:

    因为你在路由之后使用你的 express.json 中间件,所以改变这个:

    const express = require("express");
    const cors = require("cors");
    
    const mongoose = require('mongoose');
    const AppData = require("./model/AppData");
    const uri = "mongodb://localhost:27017/lunch"
    mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });
    const connection = mongoose.connection;
    
    const router = require("./routes/index");
    const PORT = 3005;
    const app = express();
    
    app.use(cors()); 
    app.use(express.raw({type: "application/json"}));
    app.use(express.json({strict: false}));
    app.use("/", router); // this need to be here
    
    connection.once('open', () => {
        console.log('?Successfully connected to MongoDB?');
    });
    
    app.listen(PORT, function () {
        console.log(`?The Backend Server is up and running on port ${PORT}?`);
    });
    

    首先是中间件,然后是路由(取决于您使用的中间件)。

    【讨论】:

    • 工作出色!谢谢!我很快就需要用更细的齿梳浏览文档……现在我处于“完成”模式:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2016-02-15
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多