I am currently learning node and express. But when I require and use body-parser my code editor(VS Code) says that it is deprecated. How do I work with this? I will link the image of my editor.
Body parser is now added to Express. You can use it like the following:
app.use(express.json());
You can add this middleware to the code which will then be able to use json methods.
If you facing 'bodyParser' is deprecated.
Just do
app.use(express.urlencoded({extended: true}));
app.use(express.json());
Note: If you are using 4.16.0
or later
express version.
body parser package is deprecated. If you are using latest version of express you don't have to install body-parser package.
You can directly use
app.use(express.urlencoded({extended:true});
Body parser is now added to Express. You can use it like the following:
app.use(express.json());
You can add this middleware to the code which will then be able to use json methods.
Don't use body-parser
Body parsing is now builtin with express
So, simply do
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 get the POST content of the request using req.body
app.post("/yourpath", (req, res)=>{
var postData = req.body;
//Or like this, for string JSON body
var postData = JSON.parse(req.body);
});
© 2022 - 2024 — McMap. All rights reserved.