📜  bodyparser 已弃用 (1)

📅  最后修改于: 2023-12-03 14:39:32.500000             🧑  作者: Mango

Deprecation of BodyParser

As a developer, you might have used the BodyParser middleware in your Node.js web application to parse incoming request bodies. However, you should be aware that BodyParser has been deprecated as of version 4.17.0 of the Express web framework.

What is BodyParser?

BodyParser is a middleware that helps to parse the body of incoming requests, such as form data, JSON payloads, and URL-encoded data. It can be used in combination with Express or other web frameworks.

Why has BodyParser been deprecated?

BodyParser has been deprecated in favor of other middleware modules, such as express.json() and express.urlencoded(), that are now included in Express by default. This simplifies the setup process for developers and reduces the maintenance burden for the Express team.

What should you use instead of BodyParser?

If you're using Express version 4.16 or later, you can use the built-in express.json() and express.urlencoded() middleware instead of BodyParser. Here's an example of how to use these middleware modules in your Express application:

const express = require('express');
const app = express();

app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
Conclusion

In summary, if you're still using BodyParser in your Node.js web application, it's time to switch to the built-in middleware modules provided by Express. This will ensure that your application remains up-to-date and avoids any issues caused by using deprecated modules.