TSLint 抱怨“不推荐使用 Express bodyParser”

问题描述

希望摆脱警告 bodyParser is deprecated. (deprecation)tslint(1) 我真的不想禁用下一行,有更好的方法吗?

这是我的 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}/`);
});

这是 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);

解决方法

我建议您使用 express 本身的内置正文解析器库,除非您有充分的理由使用自 2019 年起已弃用的 body-parser

而不是做:

app.use(bodyParser.json());

简单做

app.use(express.json());

另外在旁注中,tslint 也已弃用。现在,即使使用打字稿,您也应该使用 eslint :)

,

您可以通过将 tslint.json 文件中的 <IonItem v-for="vehicle in vehicleList" v-bind:key="vehicle.vehicle_id"> <IonLabel> <h2>{{ vehicle.manufacturer }} {{ vehicle.model }}</h2> <p>{{ vehicle.display_name }}</p></IonLabel> <IonCheckbox slot="end" v-model="vehicle.checked" @click="checkIfAllDeselected" @update="this.updateCheckboxOnClick(vehicle.vehicle_id)"/> </IonItem> 键设置为 "deprecation" 来禁用此 tslint 规则

示例 tslint.json 文件:

false

但是请注意,这将禁用每个弃用通知。如果您只想禁用下一行的弃用通知,您可以使用带有规则名称的注释标志。

{
  "deprecation": false
}
,

不要再使用 body-parser

如果您使用的是 Express 4.16+,则 express 内置了正文解析功能

你使用

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

直接快递

因此您可以使用 npm uninstall body-parser 卸载 body-parser,然后使用 express 方法。