

{"id":103584,"date":"2021-11-30T09:00:22","date_gmt":"2021-11-30T03:30:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=103584"},"modified":"2021-11-30T22:44:30","modified_gmt":"2021-11-30T17:14:30","slug":"nodejs-express-framework","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/","title":{"rendered":"Nodejs Express Framework"},"content":{"rendered":"<p>In this article, we will be discussing express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.<\/p>\n<h3>What is Nodejs express?<\/h3>\n<p>It is a framework for nodejs. It is useful to develop web and mobile applications. Express allows you to dynamically render html pages. It also allows you to perform different actions based on different html methods.<\/p>\n<h3>Why use express? What are its advantages?<\/h3>\n<ul>\n<li>It has fast I\/O<\/li>\n<li>Single threaded<\/li>\n<li>It makes Routing easy<\/li>\n<\/ul>\n<h3>How to Install Nodejs express:<\/h3>\n<p>To install express in your system run the below command:<\/p>\n<p><strong>npm install express &#8211;save<\/strong><\/p>\n<h3>How to use express?<\/h3>\n<p>To use express in your file, you have to include it using the require function.<\/p>\n<p><strong>let express=require(\u2018express\u2019)<\/strong><\/p>\n<h3>Nodejs Request &amp; Response:<\/h3>\n<p><strong>Request<\/strong>: It is the http request that contains query string, header, body and other parameters.<\/p>\n<p><strong>Response<\/strong>: Once a http request is received by express it sends a http response corresponding to the http request.<\/p>\n<h3>Nodejs Middleware:<\/h3>\n<p>It is a function having access to request and response objects. Middleware is executed sequentially. It increases the functionality of the application.<\/p>\n<p>A common middleware is a cookie parser which is discussed at the end of this article.<\/p>\n<h3>Creating a web server with Routing:<\/h3>\n<p>In the previous article we have created our web server using http, now we will be using the express framework to set up our server, we will also add routing so that our server can handle the incoming requests.<\/p>\n<p>Routing basically means how an application will respond to a given request by the client.<\/p>\n<h4>Code for creating a web server with a get route:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nlet app = express();\r\napp.get('\/', function (req, res) {\r\nres.send('Welcome to DataFlair');\r\n})\r\napp.listen(3000, function () {\r\nconsole.log(\"Server is running on port 3000\")\r\n})<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-webserver.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103969\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-webserver.webp\" alt=\"nodejs webserver\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-webserver.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-webserver-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Serving static files in Nodejs:<\/h3>\n<p>In order to serve the static file such as images, css or javascript files we will be using a middleware known as express.static.<\/p>\n<h4>Code for serving static files:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nlet app = express();\r\napp.use(express.static('public'))\r\napp.get('\/', function (req, res) {\r\nres.send('Welcome to DataFlair');\r\n})\r\napp.listen(3000, function () {\r\nconsole.log(\"Server is running on port 3000\")\r\n})<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-serving-static-file.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103970\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-serving-static-file.webp\" alt=\"nodejs serving static file\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-serving-static-file.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-serving-static-file-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Now you can see the contents of your static files.<\/p>\n<h3>Routing:<\/h3>\n<p>Routing is the way by which the application knows what to respond for the given endpoint. Below we have discussed the different types of routing like get, post in detail.<\/p>\n<h3>Nodejs GET Method:<\/h3>\n<p>Whenever we want to get something from the server we use the get method to send our request. The application then sends a response for the request.<\/p>\n<h4>Code for get method:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nlet app = express();\r\napp.use(express.static('public'))\r\napp.get('\/', function (req, res) {\r\nres.send('Welcome to DataFlair');\r\n})\r\napp.get('\/topic', function (req, res) {\r\nres.send('This is a express framework article by DataFlair');\r\n})\r\napp.listen(3000, function () {\r\nconsole.log(\"Server is running on port 3000\")\r\n})<\/pre>\n<p>Now if you go to localhost:3000 you will see \u201cWelcome to DataFlair &#8221;, and if you go to.localhost:3000\/topic you will get the topic name.<\/p>\n<h3>Post Method:<\/h3>\n<p>This method is useful when data is sent to the server from the client. In the below example code you can see that a user has sent a name.<\/p>\n<h4>Code for post method:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const express = require('express');\r\nlet app = express()\r\napp.use(express.urlencoded({\r\nextended: true\r\n}));\r\napp.get('\/', function (req, res) {\r\nres.writeHead(200, { 'Content-Type': 'text\/html' });\r\nres.write('&lt;form action=\"http:\/\/localhost:3000\" method=\"post\"&gt;');\r\nres.write('&lt;input type=\"text\" name=\"name\" placeholder=\"type your name\"&gt;&lt;br&gt;');\r\nres.write('&lt;input type=\"submit\"&gt;');\r\nres.write('&lt;\/form&gt;');\r\nres.send()\r\n})\r\napp.post('\/', function (req, res) {\r\nconsole.log(req.body.name)\r\nres.end()\r\n})\r\napp.listen(3000, function () {\r\nconsole.log(\"Server is running on port 3000\")\r\n})<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103971\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-method.webp\" alt=\"nodejs get method\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-method-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-methods.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103972\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-methods.webp\" alt=\"nodejs post methods\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-methods.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-post-methods-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Cookies Management:<\/h3>\n<p>We can send cookies to the server and then the cookie will be handled by the middleware.<\/p>\n<h4>Code for cookie management<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let express= require('express')\r\nlet cookieParser = require('cookie-parser')\r\nlet app = express()\r\napp.use(cookieParser())\r\n\r\napp.get('\/', function(req, res) {\r\nconsole.log(\"Cookies: \", req.cookies)\r\n})\r\napp.listen(3000)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Cookies: [Object: null prototype] {}<\/div>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have seen an express framework with codes and examples in detail. For other topics related to nodejs do check the DataFlair website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be discussing express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc. What is Nodejs express? It is a framework for nodejs.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":103961,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25789,25790],"class_list":["post-103584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-express","tag-nodejs-express-framework"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Express Framework - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.\" \/>\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\/nodejs-express-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Express Framework - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/\" \/>\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=\"2021-11-30T03:30:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-30T17:14:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.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":"Nodejs Express Framework - DataFlair","description":"Learn about express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.","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\/nodejs-express-framework\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Express Framework - DataFlair","og_description":"Learn about express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-11-30T03:30:22+00:00","article_modified_time":"2021-11-30T17:14:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.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\/nodejs-express-framework\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Express Framework","datePublished":"2021-11-30T03:30:22+00:00","dateModified":"2021-11-30T17:14:30+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/"},"wordCount":502,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.webp","keywords":["Nodejs Express","Nodejs Express Framework"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/","name":"Nodejs Express Framework - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.webp","datePublished":"2021-11-30T03:30:22+00:00","dateModified":"2021-11-30T17:14:30+00:00","description":"Learn about express framework in Nodejs, what it is, how to install, routing, creating web servers using express etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-express-framework.webp","width":1200,"height":628,"caption":"nodejs express framework"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-express-framework\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Express Framework"}]},{"@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\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103584","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=103584"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103584\/revisions"}],"predecessor-version":[{"id":103974,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103584\/revisions\/103974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/103961"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=103584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=103584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=103584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}