{"id":250,"date":"2017-12-14T11:01:52","date_gmt":"2017-12-14T11:01:52","guid":{"rendered":"http:\/\/blog.amt.in\/?p=250"},"modified":"2017-12-14T11:01:52","modified_gmt":"2017-12-14T11:01:52","slug":"introduction-to-express-js","status":"publish","type":"post","link":"https:\/\/blog.amt.in\/index.php\/2017\/12\/14\/introduction-to-express-js\/","title":{"rendered":"Introduction to Express.js"},"content":{"rendered":"<p>Express.js, or simply\u00c2\u00a0Express, is a\u00c2\u00a0web application framework\u00c2\u00a0for\u00c2\u00a0Node.js, released as\u00c2\u00a0free and open-source software.\u00c2\u00a0It is designed for building\u00c2\u00a0web applications\u00c2\u00a0and\u00c2\u00a0APIs.\u00c2\u00a0It is in fact the standard server framework for Node.js.<\/p>\n<p>Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is an open source framework developed and maintained by the Node.js foundation.\u00c2\u00a0Express provides a minimal interface to build our applications. It provides us the tools that are required to build our app.\u00c2\u00a0It is flexible as there are numerous modules available on\u00c2\u00a0npm, which can be directly plugged into Express.<\/p>\n<p>Node Package Manager (npm) is the package manager for node. The npm Registry is a public collection of packages of open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless other needs of the JavaScript community. npm allows us to access all these packages and install them locally.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-252\" src=\"http:\/\/blog.amt.in\/wp-content\/uploads\/2017\/12\/82671f47-2f4b-476a-83f1-4384a9c6b594-Edited-version-2-300x169.png\" alt=\"\" width=\"551\" height=\"310\" srcset=\"https:\/\/blog.amt.in\/wp-content\/uploads\/2017\/12\/82671f47-2f4b-476a-83f1-4384a9c6b594-Edited-version-2-300x169.png 300w, https:\/\/blog.amt.in\/wp-content\/uploads\/2017\/12\/82671f47-2f4b-476a-83f1-4384a9c6b594-Edited-version-2-768x432.png 768w, https:\/\/blog.amt.in\/wp-content\/uploads\/2017\/12\/82671f47-2f4b-476a-83f1-4384a9c6b594-Edited-version-2-1024x576.png 1024w, https:\/\/blog.amt.in\/wp-content\/uploads\/2017\/12\/82671f47-2f4b-476a-83f1-4384a9c6b594-Edited-version-2.png 1280w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/p>\n<p>There are two ways to install a package using npm: globally and locally.<\/p>\n<ul class=\"list\">\n<li><b>Globally<\/b>\u00c2\u00a0\u00e2\u02c6\u2019 This method is generally used to install development tools and CLI based packages. To install a package globally, use the following code.<\/li>\n<li><b>Locally<\/b>\u00c2\u00a0\u00e2\u02c6\u2019 This method is generally used to install frameworks and libraries. A locally installed package can be used only within the directory it is installed. To install a package locally, use the same command as above without the\u00c2\u00a0<b>-g<\/b>\u00c2\u00a0flag.<\/li>\n<\/ul>\n<p>The first line imports Express in our file, we have access to it through the variable Express. We use it to create an application and assign it to var app.<\/p>\n<h3>app.get(route, callback)<\/h3>\n<p>This function tells what to do when a\u00c2\u00a0get\u00c2\u00a0request at the given route is called. The callback function has 2 parameters,\u00c2\u00a0request(req)\u00c2\u00a0and\u00c2\u00a0response(res). The request\u00c2\u00a0object(req)\u00c2\u00a0represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc. Similarly, the response object represents the HTTP response that the Express app sends when it receives an HTTP request.<\/p>\n<h3>res.send()<\/h3>\n<p>This function takes an object as input and it sends this to the requesting client. Here we are sending the string\u00c2\u00a0&#8220;Hello World!&#8221;.<\/p>\n<h3>app.listen(port, [host], [backlog], [callback]])<\/h3>\n<p>This function binds and listens for connections on the specified host and port. Port is the only required parameter here.<\/p>\n<p>The above mentioned is a brief about Express.js. Watch out this space for more information about the latest trends in Technology.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Express.js, or simply\u00c2\u00a0Express, is a\u00c2\u00a0web<\/p>\n","protected":false},"author":1,"featured_media":251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83,47,7],"tags":[84,13,18],"class_list":["post-250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express-js","category-node-js","category-techtrends","tag-express-js","tag-node-js","tag-technology"],"_links":{"self":[{"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":1,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions\/253"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/media\/251"}],"wp:attachment":[{"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.amt.in\/index.php\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}