Create a web application using Node and Express framework

[Social9_Share type="horizontal"]

Create a web application using Node and Express framework

In this post, lets learn how to create a simple web project using Node js and Express 4.0 framework.
Express is a flexible Node js web application framework and provides a set of robust features for web and mobile applications.

Before jumping into this, you need to do certain prerequisites. If you have not installed Node click here to install Node. Then also go through the short explanation on NPM – Node Package Manager.

Creating a web project using Node and Express js(4.13.1) can be done in two ways.

Using npm init

In this post, let us create the project using npm init. Lets get started.

Open command prompt. Create a new folder for the node project in the location of your choice. Navigate to the folder, and then type

This will ask a series of questions related to the project. you can specify your project name. I am giving the name as sampleapp. you also have to specify the entry point (js file)of your application. you can keep the default one(index.js) or give a different name. I am changing this to app.js. For the rest of the parameters, I am keeping the default values. These values are taken in and NPM creates package.json file inside the project folder. The screen shot is given below.


Once done, you can see package.json file, inside your project folder. The default values can be modified and the dependencies needed for your project can be added here.

To add dependencies, let me modify the package.json file by adding express dependency to my project.


Save the file. Move back to command line and type

The module express and its dependencies are installed automatically.


You can also add and install the dependencies required for the project from the command line directly. Say, I want to add one more dependency module http-server. Move to command line type,

The screenshot is given below.


This updates the package.json file automatically. By doing this way you will get the latest version of the module from the repository.The updated package.json file is given below


Once the set up is done after adding the dependencies, the next step is to create and write our app.js file.The complete project set up is shown below


In app.js, use the require() method to include express module in your application. Next, create the express instance(app) by calling the top level function express() exported by express method.

Next, let’s use

     – app is an instance of express
     – METHOD is the HTTP method like GET,POST,PUT.
     – path is the path on the server
     – handler is the callback function that takes two parameters, request and response objects. This gets executed when the route is matched.

Let me use the get method now. This is for the root(‘/’) path.

The response is send back to the browser for the requests coming to the root path(‘/’).
Next, let’s start the server and make the app instance listen on port number 3000.

The complete code of app.js is given below


Move back to the command line and run the application using

The screenshot is given below


The server is started and the express instance starts listening to port 3000. Next, open the browser and load http://localhost:3000/ . The output will be as shown below.


Let’s try with one more path as ‘/contact’.


The complete code of app.js is given below


After adding this code to app.js, rerun the application using node app.js.Go back to the browser and type http://localhost:3000/contact. The output will be as shown below


Thats it. In the next post, learn how to connect to MongoDB database from a Node application using mongoose for the same node application and do CRUD operation.

Happy Learning

Post a comment