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.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.