【问题标题】:Getting body-Parser is deprecated Warning In Vs code and not Able to get Body tried to use inbuilt body-parser of express不推荐使用获取 body-Parser 警告在 Vs 代码中并且无法获取 Body 尝试使用 express 的内置 body-parser
【发布时间】:2021-06-13 23:40:03
【问题描述】:

下面是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,但再次出现同样的错误,这里是inbuilt body-parser的功能截图

【问题讨论】:

  • 你不需要外部 body-parser 。正文解析器现在包含在 Express 中。

标签: node.js express body-parser req


【解决方案1】:

从 4.16.0+ 开始,body-parser 内置在 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

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

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

这会弃用通用的 bodyParser() 中间件导出 解析 json 和 urlencoded。 “所有”中间件非常 令人困惑,因为它听起来像是解析了所有的身体, 虽然它不做多部分,这是一种常见的身体类型。还, 两个不同中间件的争论开始 重叠,这样就很难配置了。

【讨论】:

    【解决方案2】:

    如果您正在使用此代码

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

    然后您可以将这段代码替换为

    app.use(express.urlencoded());
    

    如果你正在使用

    app.use(bodyParser.json());
    

    然后替换为

    app.use(express.json());
    

    【讨论】:

      【解决方案3】:

      不要使用正文解析器

      获取请求正文现在内置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);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 2016-07-25
        • 1970-01-01
        • 2016-10-14
        相关资源
        最近更新 更多