

{"id":114690,"date":"2023-07-13T10:14:19","date_gmt":"2023-07-13T04:44:19","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=114690"},"modified":"2023-07-13T10:14:37","modified_gmt":"2023-07-13T04:44:37","slug":"expressjs-post-request","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/","title":{"rendered":"ExpressJS Post Request"},"content":{"rendered":"<p>The HTTP POST method is used to submit data to a web server, and it is a critical feature of modern web applications. In this article, we will explore the basics of the ExpressJS POST method and how it can be used to handle incoming HTTP requests.<\/p>\n<h3>ExpressJS POST Method<\/h3>\n<p>The POST method is used to submit data to a web server, often in the form of user input. When a user fills out a form or interacts with a web application, the data they submit is sent to the server using a POST request. The server can then process the data and respond with the appropriate action or message.<\/p>\n<h3>POST Request in ExpressJS<\/h3>\n<p>In Express.js, the POST method is used to handle incoming POST requests. To define a new POST route, you can use the app.post() method. It takes two arguments: the route URL and a callback function.<\/p>\n<p><strong>Example of a basic POST route in Express.js:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nconst app = express();\r\n\r\napp.post('\/submit-form', (req, res) =&gt; {\r\n  const formData = req.body;\r\n  res.send(`Form data submitted: ${JSON.stringify(formData)}`);\r\n});\r\n<\/pre>\n<p>In this example, we define a new POST route for the &#8216;\/submit-form&#8217; URL.<\/p>\n<p>When a POST request is made to this URL, the callback function captures the submitted form data.<\/p>\n<p>Note that in order to use the req.body object, you need to use a middleware such as body-parser or express.json() to parse the incoming request body.<\/p>\n<p>You can also define POST routes that accept parameters in the URL. Here&#8217;s an example of a POST route with a parameter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">app.post('\/submit-form\/:id', (req, res) =&gt; {\r\n  const id = req.params.id;\r\n  const formData = req.body;\r\n  res.send(`Form data submitted for ID ${id}: ${JSON.stringify(formData)}`);\r\n});\r\n<\/pre>\n<p>In this example, we define a new POST route with a parameter :id.<br \/>\nWhen a POST request is made to this URL with a specific ID, the callback function captures the ID value.<br \/>\nThen it sends a response back to the client with the message &#8216;Form data submitted for ID {id}: {formData}&#8217;.<\/p>\n<h3>Handling POST Requests in ExpressJS<\/h3>\n<p>In Express.js, you can handle incoming POST requests using the post() method of the Express application object. The post() method takes two parameters: the URL path and a callback function that handles the request.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">app.post('\/login', function(req, res) {\r\n  const username = req.body.username;\r\n  const password = req.body.password;\r\n  \/\/ Authenticate the user\r\n  res.send(`Logged in as ${username}`);\r\n});\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/post-req-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-114994\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/post-req-output.webp\" alt=\"post req output\" width=\"390\" height=\"170\" \/><\/a><\/p>\n<p>In this example, we define a route for the POST method with the URL path \/login. When a client sends a POST request to this URL path, the server extracts the username and password parameters from the request body using the body property of the request object. We then authenticate the user and send a response back to the client with the username using the send() method of the response object.<\/p>\n<h3>Using Forms in ExpressJS<\/h3>\n<p>In addition to sending POST requests with JSON data in the request body, clients can also submit POST requests using HTML forms. In this case, the client sends a request with an application\/x-www-form-urlencoded content type, and the request body contains a string of key-value pairs that represent the form data.<\/p>\n<p><strong>Example of Form submissions using the body-parser middleware:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const bodyParser = require('body-parser');\r\n\r\n\/\/ parse application\/x-www-form-urlencoded\r\napp.use(bodyParser.urlencoded({ extended: false }));\r\n\r\napp.post('\/contact', function(req, res) {\r\n  const name = req.body.name;\r\n  const email = req.body.email;\r\n  const message = req.body.message;\r\n  \/\/ Send an email to the website owner\r\n  res.send(`Thank you for your message, ${name}!`);\r\n});\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/post-form-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-114995\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/post-form-output.webp\" alt=\"post form output\" width=\"499\" height=\"213\" \/><\/a><\/p>\n<p>In this example, we use the body-parser middleware to parse the request body with an application\/x-www-form-urlencoded content type. We then define a route for the POST method with the URL path \/contact. When a client submits a form to this URL path, the server extracts the name, email, and message parameters from the request body using the body property.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In conclusion, the POST method is an essential part of building dynamic web applications with Express.js. By using the body-parser middleware, developers can easily parse the request body and extract data submitted by clients through POST requests. This data can then be used to update databases, modify server-side data, or return dynamic responses to clients.<\/p>\n<p>Using the POST method, developers can build powerful applications that allow users to interact with the server and perform complex tasks, such as submitting forms, uploading files, or processing payments. With the help of Express.js, developers can build scalable and secure web applications that can handle high traffic and meet the needs of modern web users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The HTTP POST method is used to submit data to a web server, and it is a critical feature of modern web applications. In this article, we will explore the basics of the ExpressJS&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":114804,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27631],"tags":[27688,27687],"class_list":["post-114690","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express-js-tutorials","tag-expressjs-post-method","tag-expressjs-post-request"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ExpressJS Post Request - DataFlair<\/title>\n<meta name=\"description\" content=\"The POST method is an essential part of building dynamic web applications with Express.js. Learn about Post Request in ExpressJS with example\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ExpressJS Post Request - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The POST method is an essential part of building dynamic web applications with Express.js. Learn about Post Request in ExpressJS with example\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T04:44:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-13T04:44:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ExpressJS Post Request - DataFlair","description":"The POST method is an essential part of building dynamic web applications with Express.js. Learn about Post Request in ExpressJS with example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/","og_locale":"en_US","og_type":"article","og_title":"ExpressJS Post Request - DataFlair","og_description":"The POST method is an essential part of building dynamic web applications with Express.js. Learn about Post Request in ExpressJS with example","og_url":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-07-13T04:44:19+00:00","article_modified_time":"2023-07-13T04:44:37+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"ExpressJS Post Request","datePublished":"2023-07-13T04:44:19+00:00","dateModified":"2023-07-13T04:44:37+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/"},"wordCount":650,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp","keywords":["ExpressJS Post Method","ExpressJS Post Request"],"articleSection":["Express JS Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/","url":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/","name":"ExpressJS Post Request - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp","datePublished":"2023-07-13T04:44:19+00:00","dateModified":"2023-07-13T04:44:37+00:00","description":"The POST method is an essential part of building dynamic web applications with Express.js. Learn about Post Request in ExpressJS with example","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/expressjs-post-request\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/04\/expressjs-post-method.webp","width":1200,"height":628,"caption":"expressjs post method"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/expressjs-post-request\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Express JS Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/express-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"ExpressJS Post Request"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/114690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=114690"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/114690\/revisions"}],"predecessor-version":[{"id":114996,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/114690\/revisions\/114996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/114804"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=114690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=114690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=114690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}