不推荐使用获取正文解析器警告在 Vs 代码中并且无法获取正文尝试使用 express 的内置正文解析器

Ary*_*rya 9 node.js express body-parser req

在此处输入图片说明

下面是app.js文件的代码

var port = process.env.PORT || 8080; // set our port
var express = require('express');
var app = express();
var bodyParser = require('body-parser')
var cors = require('cors');
var indexRouter = require("./server/routes/index");
var http = require('http').Server(app);
const path = require('path')

const Licence = require('./server/CronJob/CronJob');

http.listen(port);

app.use(cors());

app.use(bodyParser.json({ limit: '50mb' }));

app.use(bodyParser.json({
    type: 'application/vnd.api+json'
}));

app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true,
    parameterLimit: 50000
}));


// parse application/json
app.use(express.json());
app.use(express.urlencoded()); //Parse URL-encoded bodies


app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", '*');
    res.header("Access-Control-Allow-Credentials", true);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
    res.header("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0");
    next();
});

app.use(express.static(path.join(__dirname, "/build")));

app.use(indexRouter);

app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, '/build/index.html'), function (err) {
        if (err) {
            res.status(500).send(err)
        }
    })
})

// Licence.licenceExpire();

console.log('Magic happens on port ' + port); // shoutout to the user

exports = module.exports = app;
Run Code Online (Sandbox Code Playgroud)

版本

表达:^4.17.1,正文解析器:^1.19.0

并且还使用了下面博客中给出的建议

更新

我使用了内置的 body-parser 但这里再次出现同样的错误是内置 body-parser 的功能截图

在此处输入图片说明

Tus*_*har 10

从 4.16.0+ 开始,body-parser 内置于 express

使用http://expressjs.com/en/api.html#express.json

app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded())
Run Code Online (Sandbox Code Playgroud)

https://expressjs.com/en/changelog/4x.html#4.16.0

已添加 express.json() 和 express.urlencoded() 中间件以提供开箱即用的请求正文解析支持。这使用下面的 expressjs/body-parser 模块模块,因此当前需要单独使用该模块的应用程序可以切换到内置解析器。

https://github.com/expressjs/body-parser/commit/b7420f8dc5c8b17a277c9e50d72bbaf3086a3900

这弃用了解析 json 和 urlencoded 的通用 bodyParser() 中间件导出。“all”中间件非常令人困惑,因为它听起来像是解析所有主体,尽管它不做多部分,这是一种常见的主体类型。此外,两个不同的中间件的参数开始重叠,并且在以这种方式完成时很难配置。