Bill
Bill

Reputation: 5150

TSLint complaining that 'Express bodyParser is deprecated'

Hoe to get rid of the warning bodyParser is deprecated. (deprecation)tslint(1) I don't really want to disable next line, is there a better way?

This is my index.ts

import { app } from './app';
import * as dotenv from 'dotenv';
dotenv.config();

const hostname = process.env.HOST;
const port = process.env.PORT;

app.listen(port, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

and this is app.ts

import express, { Application } from 'express';
import bodyParser from 'body-parser';
import { routes } from './routes';
export const app: Application = express();


app.use(bodyParser.json()); // bodyParser is deprecated. (deprecation)tslint(1)
routes(app);

Upvotes: 0

Views: 375

Answers (3)

Abraham
Abraham

Reputation: 15750

Do not use body-parser anymore

If you are using Express 4.16+ the body parsing functionality has come with express as builtin

You use

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

from directly express

so you can uninstall body-parser using npm uninstall body-parser, and simply go with the express method.

Upvotes: 0

Sorikairo
Sorikairo

Reputation: 798

I recommend that you use the built-in body parser lib from express itself excepted if you have REALLY good reason to use body-parser which is deprecated since 2019.

instead of doing :

app.use(bodyParser.json());

simply do

app.use(express.json());

Also on a sidenote, tslint is also deprecated. Now, you should use eslint even when using typescript :)

Upvotes: 3

Mats Jun
Mats Jun

Reputation: 351

You can disable this tslint rule by setting the "deprecation" key to false in your tslint.json file

Example tslint.json file:

{
  "deprecation": false
}

Do however notice that this will disable every single deprecation notice. If you want to only disable the deprecation notice for the next line you can use a comment flag with rule names.

// tslint:disable-next-line:deprecation
app.use(bodyParser.json());
routes(app);

Upvotes: 0

Related Questions