简体   繁体   中英

Is it the same to use express.json than bodyParser?

I have a question that is not clear to me yet. Is it the same to use express.json() as to use body-parser module? What is the difference? How does one benefit from each other? Right now I am creating an API that returns json and after reading the documentation it has not been very clear to me.

They essentially do the same thing. When Express 4.0 was released, they removed some of the middleware that used to come with express (eg express.json). This meant we needed to get our own Json parsing middleware - bodyparser, allowing us to then use app.use(bodyParser.json()) instead of the old/built-in app.use(express.json()) that would no longer work.

As these patch notes for Express 4.1.16 (2nd bullet) shows, they added back in so that express now has that functionality again. You could now use either. I believe both ways ( body-parser.json() and express.json() ) should function identically if you have the newest version of Node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM