In our package.json file, we have specified mainindex.js. So, to start developing our hello world in Express JS, create file index.js and type the following code.
//imports express module
var express = require('express');
//initializes express app
var app = express();
//creates get function with request and response parameters
app.get('/', function(request, response){
//sends response to client or browser
response.send("Hello World");
});
//listens to server at port 3000
app.listen(3000);
In the above code, lines starting with // mentions comments for each line of code to explain what a line of code does. You can see we have set app listen to port 3000 but if you face any error on this port, it means you have something running in background on the same port or this port has been blocked by firewalls. So, don’t worry. You may resolve this by closing that background process, unblocking the port in firewalls or changing the port to some other like port 80, port 8080, port 8081 etc. Now save index.js file after adding above code.
Run Nodemon for starting Express JS Server
How to start Express JS server? To run Express JS server, you need to execute the following command:
$ nodemon index.js
We have used nodeman to start Express server because we don’t want to waste our time again and again in starting server after app crash during development. And we also don’t want to start server again after making a change. Nodeman will automatically handle this issue by restarting automatically whenever a change is made to the code. If the server has started successfully, you will see the identical screen as shown below.
Now you can access the Express app through your browser by accessing http://localhost:3000. If you changed port, change the port number in URL as well. After accessing the link, you will see the screen as shown below.
How does Express JS app work?
The following line of code imports Express JS in our file so that we can access Express modules.
var express = require('express');
The following line of code initializes the app variable by creating Express application.
var app = express();
To declare a variable, you will may use var or let keyword. The code below is similar to the lines give above.
//imports express module
let express = require('express');
//initializes express app
let app = express();
app.get (route, callback)
All the parameters of this function are required. This is a function which helps us in mentioning route for making URLs and function which returns the response after processing the request sent by this URL. It allows get and post methods. I will explain post request type later. GET is HTTP request which is used mostly to fetch some data from database. Route specifies the URL which you access on browser. Callback is a function which has two parameter request and response. Request object represents HTTP requests sent to server via client and response object represents HTTP response sent by Express JS server in response to the HTTP request.
Parameter’s Description
route
It specifies the URL string to send a request through client.
callback
A function which receives request and sends response.
response.send()
This function sends the response for the received HTTP request. In this program, we have sent “Hello World” string in response to GET HTTP request.
app.listen (port, [host], [backlog], [callback])
All the parameters except port number are optional in this function because port is necessary for accessing the app through client. Backlog keeps record of maximum pending requests in queue. Callback is a function which executes when server starts. Hostis the domain name which is set while deploying app on server.
Parameter’s
Description
Callback
The function which executes after server has started listening to port. This is optional parameter.
Host
This parameter is optional and set when application is deployed on a server.
Backlog
This is optional parameter and it holds the maximum pending queued requests. It’s default value is 511.
Port
This is required parameter. This specifies the port number on which server will accept requests.
In the next tutorial, you will learn about the requests available in Express JS and how to use them.