Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

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

enter image description here

Below Is Code Of app.js file

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;

Version

express: ^4.17.1, body-parser: ^1.19.0

and also used suggestion given in below blog

update

I have used inbuilt body-parser but getting same error again here is the screenshot of function of inbuilt body-parser

enter image description here

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

From 4.16.0+ body-parser is inbuilt in express

Use 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.

over 4 years ago · Santiago Trujillo Relatório

0

If you are using this code

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

then you can replace this code with

app.use(express.urlencoded());

and if you are using

app.use(bodyParser.json());

then replace with

app.use(express.json());
over 4 years ago · Santiago Trujillo Relatório

0

Don't use body-parser

Getting request body is now builtin with express So, you can simply use

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

directly from express

You can uninstall body-parser using npm uninstall body-parser



Then you can simply get the POST content from req.body

app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or just like this, for string JSON body

    var postData = JSON.parse(req.body);
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda