{"id":102,"date":"2019-10-28T21:50:56","date_gmt":"2019-10-28T21:50:56","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/nodejs\/?p=102"},"modified":"2019-10-28T21:50:56","modified_gmt":"2019-10-28T21:50:56","slug":"node-js-express-framework","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/nodejs\/node-js-express-framework\/","title":{"rendered":"Node.js &#8211; Express Framework"},"content":{"rendered":"<p>In this tutorial, we are going to be talking about Express Framework in Node.js.<\/p>\n<p>You will learn what the Express framework is. You&#8217;ll also learn how to set up and use Express Framework.<\/p>\n<ol>\n<li><a href=\"#t1\">Overview of Express Framework<\/a><\/li>\n<li><a href=\"#t2\">Setting up Express Framework<\/a><\/li>\n<li><a href=\"#t3\">A Simple Example<\/a><\/li>\n<li><a href=\"#t4\">Request and Response<\/a><\/li>\n<li><a href=\"#t5\">Basic Routing<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Overview of Express Framework<\/strong><\/h5>\n<p>The Express Framework also called express.js is a web application server framework that works with Node.js. You can used it to quickly build web applications like single-page applications and mobile applications.<\/p>\n<p>Features of the express framework include:<\/p>\n<ul>\n<li>can server as template engine for rendering html pages<\/li>\n<li>provides routing table for managing http requests<\/li>\n<li>enables setting up of middleware for responding to http request<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">\u00a02. Setting up Express Framework<\/strong><\/h5>\n<p>To set up express simply run the command<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm install express\r\n<\/pre>\n<p>After running this command, a folder is created in your application folder containing all the downloaded modules.<\/p>\n<p>You&#8217;ll also need to install the following additional modules:<\/p>\n<ul>\n<li>body-parser &#8211; for handling different format type including json, row, text and url-encoded form data<\/li>\n<li>cookie-parser &#8211; used or parsing cookie header\u00a0 and getting cookie values<\/li>\n<li>multer &#8211; for handling form data<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. A simple Example<\/strong><\/h5>\n<p>Let&#8217;s write a simple express app that creates and starts a web server listening at port 8080. When a request comes from a client, the app responds with a message &#8216;Welcome to Express Framework&#8217;.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Demo Express App by Kindson The Genius<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> express <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'express'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> app <span style=\"color: #333333;\">=<\/span> express();\r\n\r\napp.get(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(req, res){\r\n    res.send(<span style=\"background-color: #fff0f0;\">'Welcome to Express'<\/span>);\r\n})\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> server <span style=\"color: #333333;\">=<\/span> app.listen(<span style=\"color: #0000dd; font-weight: bold;\">8080<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(){\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> host <span style=\"color: #333333;\">=<\/span> server.address().address\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> port <span style=\"color: #333333;\">=<\/span> server.address().port\r\n\r\n    console.log(<span style=\"background-color: #fff0f0;\">\"Demo web app listening at http:\/\/%s:%s\"<\/span>, host, port)\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Run the code above. The visit http:\/\/localhost:8080, then you can see the page displays with the message &#8216;Welcome to Express&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Request and Response in Express<\/strong><\/h5>\n<p>An application create with express makes use of callback function. The function would take two parameters: request and response. This is as shown below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.get(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(req, res){\r\n    res.send(<span style=\"background-color: #fff0f0;\">'Welcome to Express'<\/span>);\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<ul>\n<li>A request object represents a http request coming from a client. It has all the properties of a request including url query string, request body, http header\u00a0 and body<\/li>\n<li>A response object represents\u00a0 a http response provided by the server<\/li>\n<\/ul>\n<p>You can also obtain the properties of the request and response objects to examine them or print them out to the console<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t5\">5. Routing Using Express<\/strong><\/h5>\n<p>Now we would talk about something very important: routing.<\/p>\n<p>How do control the endpoint that is reached based on the url path provided?\u00a0 Also routing helps us route a request based on the http method. For instance a POST request, GET request, PUT or DELETE request.<\/p>\n<p>Let&#8217;s work with our previous demo webapp. This time, we&#8217;ll extend it to include routing feature. This is as easy as just changing app.get() to app.post() and a little more. Let&#8217;s do it<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Demo Express App by Kindson The Genius<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> express <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'express'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> app <span style=\"color: #333333;\">=<\/span> express();\r\n\r\n<span style=\"color: #888888;\">\/\/ Handles a \"GET Request for home\" on the homepage<\/span>\r\napp.get(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (req, res) {\r\n   console.log(<span style=\"background-color: #fff0f0;\">\"GET request for the homepage\"<\/span>);\r\n   res.send(<span style=\"background-color: #fff0f0;\">'Welcome to GET!'<\/span>);\r\n})\r\n\r\n<span style=\"color: #888888;\">\/\/ Handles a POST request for the homepage<\/span>\r\napp.post(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (req, res) {\r\n   console.log(<span style=\"background-color: #fff0f0;\">\"POST request for the homepage\"<\/span>);\r\n   res.send(<span style=\"background-color: #fff0f0;\">'Welcome to POST!'<\/span>);\r\n})\r\n\r\n<span style=\"color: #888888;\">\/\/ Handles a DELETE request for the \/del_user page.<\/span>\r\napp.<span style=\"color: #008800; font-weight: bold;\">delete<\/span>(<span style=\"background-color: #fff0f0;\">'\/del_user'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (req, res) {\r\n   console.log(<span style=\"background-color: #fff0f0;\">\"DELETE request coming from \/del_user\"<\/span>);\r\n   res.send(<span style=\"background-color: #fff0f0;\">'Welcome to DELETE!'<\/span>);\r\n})\r\n\r\n<span style=\"color: #888888;\">\/\/ Handles a GET request for the \/list_user page.<\/span>\r\napp.get(<span style=\"background-color: #fff0f0;\">'\/list_user'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (req, res) {\r\n   console.log(<span style=\"background-color: #fff0f0;\">\"GET Request comming from \/list_user\"<\/span>);\r\n   res.send(<span style=\"background-color: #fff0f0;\">'List of User'<\/span>);\r\n})\r\n\r\n<span style=\"color: #888888;\">\/\/ Handles a get request whose part correspond to the url pattern<\/span>\r\n<span style=\"color: #888888;\">\/\/wxyz, wxayz, wxmyyz etc<\/span>\r\napp.get(<span style=\"background-color: #fff0f0;\">'\/wx*yz'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(req, res) {   \r\n   console.log(<span style=\"background-color: #fff0f0;\">\"GET Request comming from \/wx*yz\"<\/span>);\r\n   res.send(<span style=\"background-color: #fff0f0;\">'URL Pattern Match'<\/span>);\r\n})\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> server <span style=\"color: #333333;\">=<\/span> app.listen(<span style=\"color: #0000dd; font-weight: bold;\">8080<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> () {\r\n   <span style=\"color: #008800; font-weight: bold;\">var<\/span> host <span style=\"color: #333333;\">=<\/span> server.address().address\r\n   <span style=\"color: #008800; font-weight: bold;\">var<\/span> port <span style=\"color: #333333;\">=<\/span> server.address().port\r\n   \r\n   console.log(<span style=\"background-color: #fff0f0;\">\"Demo app listening at http:\/\/%s:%s\"<\/span>, host, port)\r\n})\r\n<\/pre>\n<p>Save the code and then run it.<\/p>\n<p>Try to visit these urls and observe the output<\/p>\n<ul>\n<li>http:\/\/localhost\/8080<\/li>\n<li>http:\/\/localhost\/list_user<\/li>\n<\/ul>\n<p>In the next part, I will teach you how to use Advanced REST Client to make DELETE and POST requests<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to be talking about Express Framework in Node.js. You will learn what the Express framework is. You&#8217;ll also learn &hellip; <\/p>\n","protected":false},"author":1,"featured_media":106,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/102\/revisions\/107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media\/106"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}