{"id":11039,"date":"2020-09-07T19:36:38","date_gmt":"2020-09-07T23:36:38","guid":{"rendered":"https:\/\/www.carnaghan.com\/?p=11039"},"modified":"2020-09-07T19:51:54","modified_gmt":"2020-09-07T23:51:54","slug":"quick-intro-to-express-js","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/quick-intro-to-express-js\/","title":{"rendered":"Quick Intro to Express.js"},"content":{"rendered":"\n
If you have ever come across Node.js, then you may have heard the word “Express.js”. Express is also a part of popular full-stack web development stacks – MEAN and MERN. M, A\/R, and N, meaning MongoDB, Angular\/React, and Node.js, respectively are very popular in the web development community, while E, i.e. Express.js is unpopular. Many developers who work with MEAN or MERN stack don’t even know what is the exact meaning of express. So in this article, we will discuss express.js with examples.<\/p>\n\n\n\n
Before moving to express.js, let’s take a brief look at node.js. Node.js is a JavaScript runtime environment that executes JavaScript outside the browser. In simple words, Express.js is a framework for node.js. It is the most popular framework which is used to create node applications. This framework offers many features. For example, it enables a developer to create web servers with ease, better readability, and flexibility.<\/p>\n\n\n\n
The node HTTP library can also be used to create a server but is complicated. Express.js prevents such complications and lets us create servers with ease. So let’s start by creating a web server using express.js.<\/p>\n\n\n\n
Start by creating a new directory and then use the following command to create a package.json file.<\/p>\n\n\n\n
npm init --yes<\/code><\/pre>\n\n\n\nThis file, package.json is a kind of manifest for the application. It holds the metadata relevant to the project.<\/p>\n\n\n\n
Let’s install express.js. It is available in NPM.<\/p>\n\n\n\n
npm install express --save<\/code><\/pre>\n\n\n\nNow we can create a server. Let’s do it step by step.<\/p>\n\n\n\n
Create a file and name it “index.js”. To use express.js in the file, first, we need to import it.<\/p>\n\n\n\n
var express = require(\"express\")<\/code><\/pre>\n\n\n\nNext, we need the reference of express to set up the application.<\/p>\n\n\n\n
var express = require(\"express\")\nvar app = express()<\/code><\/pre>\n\n\n\nThese two lines are very important. The “app” variable will be used in the file to create the server and do other important things. Now we need the application to run on a specific port. The “listen” function will help us.<\/p>\n\n\n\n
var express = require(\"express\")\nvar app = express()\n\napp.listen(4000, () => {\n console.log('Express running successfully on port 4000');\n})\n<\/code><\/pre>\n\n\n\nThe “app.listen” has two parameters – port number and a callback function. Basically, this function informs the server to listen for connections at a particular port. In our case, the server will run on port 4000 and it will print “Express running successfully on port 4000” in the console if the connection is successful.<\/p>\n\n\n\n
Let’s execute the above code.<\/p>\n\n\n\n