{"id":22821,"date":"2024-09-24T17:09:35","date_gmt":"2024-09-24T11:39:35","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=22821"},"modified":"2025-08-06T00:40:33","modified_gmt":"2025-08-05T19:10:33","slug":"body-parser-deprecated","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/body-parser-deprecated\/","title":{"rendered":"bodyParser is deprecated in Express 4: What to do?"},"content":{"rendered":"\n<p>If you are watching old tutorials or reading some old documentation, you see the usage of a module called<strong> body-parser<\/strong> to parse the body of incoming HTTP requests. Its ability to convert raw data (like JSON, form data, etc.) into a JavaScript object and store it in <strong>req.body<\/strong>, made it super popular.<\/p>\n\n\n\n<p>It was so useful in those days that even big companies started using it and with so much popularity, Express 4.16.0 integrated its core functionality and we no longer have to rely on it. As a result,<strong> body-parser was deprecated after Express 4.16.0<\/strong>.<\/p>\n\n\n\n<p>So if now with the latest version of express, you are trying to use body-parser, you might get different types of errors related to body-parser deprecated. So let\u2019s see how to solve this by using the built-in Express parsing method.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note: <\/strong>The body-parser isn&#8217;t completely removed from npm, it&#8217;s still available but deprecated in favour of the Express built-in methods.<\/p>\n<\/blockquote>\n\n\n\n<p><a href=\"https:\/\/codeforgeek.com\/next-js-tutorials\/\">Click here to follow our exclusive 2024 Next.js series!<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Old Way: Using body-parser in Express (Before 4.16.0)<\/h2>\n\n\n\n<p>For Express version less than 4.16.0, we can still use the body-parser package in the following way to parse the body of incoming HTTP request<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installation of body-parser<\/h3>\n\n\n\n<p>To use body-parser, we have to install it using npm:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nnpm install body-parser\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Implementation<\/strong><\/h3>\n\n\n\n<p>Here&#8217;s how we can implement it in our Express application:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;);\nconst bodyParser = require(&#039;body-parser&#039;);\n\nconst app = express();\nconst port = 3000;\n\n\/\/ Parse JSON request bodies\napp.use(bodyParser.json());\n\n\/\/ Parse URL-encoded request bodies\napp.use(bodyParser.urlencoded({ extended: true }));\n\n\/\/ Define a route that handles POST requests\napp.post(&#039;\/api\/data&#039;, (req, res) =&gt; {\n  \/\/ Access the parsed data from the request body\n  const jsonData = req.body;\n\n  \/\/ Process the data or respond as needed\n  res.json({ message: &#039;Data received successfully&#039;, data: jsonData });\n});\n\n\/\/ Start the server\napp.listen(port, () =&gt; {\n  console.log(`Server is running on port ${port}`);\n});\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">The New Way: Using Built-in Body Parsing in Express (4.16.0 and Later)<\/h2>\n\n\n\n<p>As we learned, starting with Express.js version 4.16.0, the body-parser middleware has been deprecated and is no longer necessary because its functionality has been integrated into Express itself.<\/p>\n\n\n\n<p>You can now use<strong> express.json()<\/strong> and<strong> express.urlencoded() <\/strong>methods to handle request data directly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is express.json()?<\/h3>\n\n\n\n<p>The<strong> express.json()<\/strong> is used to parse the JSON (JavaScript Object Notation) data sent in the request body.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;);\nconst app = express();\n\n\/\/ Middleware to parse JSON request bodies\napp.use(express.json());\n\n\/\/ route handling code here\n\n<\/pre><\/div>\n\n\n<p>When a request comes to the Express server with a JSON body, express.json() automatically parses the JSON data and attaches it to the req.body property. I watched their video <a href=\"https:\/\/youperv.com\" title=\"new video brazzers\" target=\"_blank\" rel=\"noopener\">https:\/\/youperv.com<\/a> new movies today <\/p>\n\n\n\n<p><strong>Example route:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\napp.post(&#039;\/api\/data&#039;, (req, res) =&gt; {\n  const jsonData = req.body; \/\/ Access the parsed JSON data here\n  \/\/code to process the JSON data\n});\n<\/pre><\/div>\n\n\n<p>The client must include the <strong>Content-Type: application\/json<\/strong> header in the HTTP request. express.json() will only parse the body as JSON if this header is set.<\/p>\n\n\n\n<p><strong>Example request:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nPOST \/api\/data\nContent-Type: application\/json\n\n{\n  &quot;key&quot;: &quot;value&quot;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">What is express.urlencoded()?<\/h3>\n\n\n\n<p>The express.urlencoded is used to parse the URL-encoded data sent in the request body<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;);\nconst app = express();\n\n\/\/ Middleware to parse URL-encoded request bodies\napp.use(express.urlencoded({ extended: true }));\n\n\/\/route handling code here\n\n<\/pre><\/div>\n\n\n<p>When a request comes to the Express server with a URL-encoded data body, express.urlencoded() also automatically parses that data and attaches it to the<strong> <\/strong>req.body property.<\/p>\n\n\n\n<p><strong>Example route:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\napp.post(&#039;\/api\/data&#039;, (req, res) =&gt; {\n  const formData = req.body; \/\/ Access the parsed URL-encoded data here\n  \/\/ Your code to process the form data\n});\n<\/pre><\/div>\n\n\n<p>The client must include the <strong>Content-Type: application\/x-www-form-urlencoded<\/strong> header in the HTTP request. express.urlencoded() will only parse the body as<strong> URL-encoded data <\/strong>if this header is set.<\/p>\n\n\n\n<p><strong>Example request:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nPOST \/api\/data\nContent-Type: application\/x-www-form-urlencoded\n\nkey1=value1&amp;key2=value2\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Implementation<\/h3>\n\n\n\n<p>Below is the updated code, replacing the use of body-parser with the built-in middleware methods express.json() and<strong> express.urlencoded().<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst express = require(&#039;express&#039;);\n\nconst app = express();\nconst port = 3000;\n\n\/\/ Middleware to parse JSON request bodies\napp.use(express.json());\n\n\/\/ Middleware to parse URL-encoded request bodies\napp.use(express.urlencoded({ extended: true }));\n\n\/\/ Define a route that handles POST requests\napp.post(&#039;\/api\/data&#039;, (req, res) =&gt; {\n    \/\/ Access the parsed data from the request body\n    const jsonData = req.body;\n\n    \/\/ Process the data or respond as needed\n    res.json({ message: &#039;Data received successfully&#039;, data: jsonData });\n});\n\n\/\/ Start the server\napp.listen(port, () =&gt; {\n    console.log(`Server is running on port ${port}`);\n});\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In short, before Express 4.16.0, it was required to use the body-parser module for parsing incoming data. After Express 4.16.0, body-parser features were added to the Express and it became deprecated. Now it is recommended to use express.json() to parse JSON data and express.urlencoded() to handle URL-encoded data.<\/p>\n\n\n\n<p><a href=\"https:\/\/codeforgeek.com\/getting-form-data-in-node-js\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/getting-form-data-in-node-js\/\">Click here to check out our in-depth guide on how to handle form data without using body-parser.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/24330014\/bodyparser-is-deprecated-express-4\" data-type=\"link\" data-id=\"https:\/\/stackoverflow.com\/questions\/24330014\/bodyparser-is-deprecated-express-4\" target=\"_blank\" rel=\"noopener\"><\/a><a href=\"https:\/\/stackoverflow.com\/questions\/24330014\/bodyparser-is-deprecated-express-4\" data-type=\"link\" data-id=\"https:\/\/stackoverflow.com\/questions\/24330014\/bodyparser-is-deprecated-express-4\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/24330014\/bodyparser-is-deprecated-express-4<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are watching old tutorials or reading some old documentation, you see the usage of a module called body-parser to parse the body of incoming HTTP requests. Its ability to convert raw data (like JSON, form data, etc.) into a JavaScript object and store it in req.body, made it super popular. It was so [&hellip;]<\/p>\n","protected":false},"author":103,"featured_media":32302,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-22821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated.png",1280,720,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated-300x169.png",300,169,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated-768x432.png",768,432,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated-1024x576.png",1024,576,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated.png",1280,720,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/bodyParser-is-deprecated.png",1280,720,false]},"uagb_author_info":{"display_name":"Arnab Biswas","author_link":"https:\/\/codeforgeek.com\/author\/arnab\/"},"uagb_comment_info":0,"uagb_excerpt":"If you are watching old tutorials or reading some old documentation, you see the usage of a module called body-parser to parse the body of incoming HTTP requests. Its ability to convert raw data (like JSON, form data, etc.) into a JavaScript object and store it in req.body, made it super popular. It was so&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=22821"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/32302"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=22821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=22821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=22821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}