Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSLint marks body-parser as deprecated

Tags:

I have the Typescript code below:

import * as express from 'express'; import * as bodyParser from 'body-parser';  ... const app: express.Application = express();  app.use(bodyParser.json());  

In VSCode the bodyParser on the last line is marked with yellow squiggles saying that body-parser is deprecated.

In the .d.ts file I see the following:

/** @deprecated */ declare function bodyParser(     options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded, ): NextHandleFunction;  declare namespace bodyParser { ...     function json(options?: OptionsJson): NextHandleFunction; 

Why is the linter complaining about the body-parser function while I do not use it as a function in my code? Am I missing something in a tsconfig.json file to prevent this? Compiling doesn't seem to be a problem.

like image 266
Halt Avatar asked Jun 15 '20 20:06

Halt


People also ask

How do you fix body parser deprecated?

To fix the 'BodyParser is deprecated' warning with Node. js and Express, we can replace bodyParser with express. urlencoded and express. json .

Is body parser deprecated?

'bodyParser' is deprecated. // If you are using Express 4.16+ you don't have to import body-parser anymore.

Is bodyParser still used?

This piece of middleware was called body-parser and used to not be part of the Express framework. The good news is that as of Express version 4.16+, their own body-parser implementation is now included in the default Express package so there is no need for you to download another dependency.

What does bodyParser deprecated mean?

Explanation: The default value of the extended option has been deprecated, meaning you need to explicitly pass true or false value. Note for Express 4.16. 0 and higher: body parser has been re-added to provide request body parsing support out-of-the-box.


2 Answers

BodyParse is built into Express js

So now you don't have to install body-parser, do this instead.

app.use(express.json()); 
like image 56
Thanveer Shah Avatar answered Sep 20 '22 13:09

Thanveer Shah


Do not use body-parser anymore

Since Express 4.16+ the body parsing functionality has become builtin with express

So, you can simply do

app.use(express.urlencoded({extended: true})); app.use(express.json()) // To parse the incoming requests with JSON payloads 

using express directly, without having to install body-parser.

uninstall body-parser using npm uninstall body-parser



Then you can access the POST data, using req.body

app.post("/yourpath", (req, res)=>{      var postData = req.body;      //Or if body comes as string,      var postData = JSON.parse(req.body); }); 
like image 27
Abraham Avatar answered Sep 17 '22 13:09

Abraham