获取 body-Parser 已弃用 Vs 代码中的警告并且无法获取 Body 尝试使用 express 的内置 body-parser

Getting body-Parser is deprecated Warning In Vs code and not Able to get Body tried to use inbuilt body-parser of express

下面是 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;

版本

快递:^4.17.1, 正文解析器:^1.19.0

并且还使用了下面给出的建议blog

更新

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

从 4.16.0+ 开始,主体解析器内置于 express

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

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

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

The express.json() and express.urlencoded() middleware have been added to provide request body parsing support out-of-the-box. This uses the expressjs/body-parser module module underneath, so apps that are currently requiring the module separately can switch to the built-in parsers.

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

This deprecates the generic bodyParser() middleware export that parses both json and urlencoded. The "all" middleware is very confusing, because it makes it sound like it parses all bodyes, though it does not do multipart, which is a common body type. Also, the arguments for the two different middleware are starting to overlap and it's hard to configure then when done this way.

如果您正在使用此代码

app.use(bodyParser.urlencoded({extended: true}));

那么您可以将此代码替换为

app.use(express.urlencoded());

如果您正在使用

app.use(bodyParser.json());

然后替换为

app.use(express.json());

不要使用 body-parser

获取请求正文现在内置于 express 因此,您可以简单地使用

app.use(express.json()) //For JSON requests
app.use(express.urlencoded({extended: true}));

直接从快递

您可以使用 npm uninstall body-parser

卸载 body-parser



然后你可以简单地从req.body

中获取POST内容
app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or just like this, for string JSON body

    var postData = JSON.parse(req.body);
});