Contact Us

Kockpit is here to help you

Business Form

Sameer Patwal

Working with Express JS | Express JS installation and more

Express JS: A brief overview

As we know Express is a Node.js web application Framework that provides a robust set of features to develop web and mobile applications. It also provides the instant development of Node-based Web Applications.

Given below are some of the core features of the Express framework-

  1. It allows to set up middlewares to respond to HTTP requests.
  2. It defines a routing table that is always used to perform a different set of actions which are based on the HTTP Method and URL.
  3. It allows users to dynamically render HTML Pages based on passing arguments to templates.

Now as a part of the learning process below is the list of steps we will follow to gain In-depth and practical knowledge of working with express.js.

  1. Installation of Express.js
  2. Creating Server Using Express.js
  3. Installation of Nodemon
  4. Handling Request and Responses (Get Request)
  5. Routing with Express.js
  6. Creating, Serving & Parsing HTML Pages.
  7. Handling POST Request

Installing Express.js

Installation of Express.js is fast and easy. It can be installed in a number of different operating systems. The key installation steps are listed below.

  • First of all, Node.js should have been installed in the system, then in cmd terminal created a directory.
  • (here all the files will be saved in a folder/dir which we will develop)

E:\>mkdir expressjswork
E:\>cd expressjswork
  • We have to use the npm init command to create a package.json file for our application.

(JSON is an open standard file format, that uses human-readable text to store and transmit data objects consisting of attribute and array data types.)

E:\expressjswork>npm init

In the next step install the Express framework globally using NPM so that it can be used to create our web application using Node Terminal.

E:\expressjswork>npm install express
E:\expressjswork>npm install express –save

The above command saves the installation locally in the node_modules directory.

Creating Server Using Express

Users can also create servers using express.js and in this step, we will create a server using Express, below is some required code that is written before creating a server for us.

const express = require("express");

Because in order to use express in our project in node we use “require”.

Now if we want to create a web application, we need to call the Express function, for which the code is given below.

const app = express();

Now finally, to create a server for us we need to use a method called “listen”, and we can create a port by it after it we will call Function and pass two parameters i.e, Request & Response.

And to check whether the server is running smoothly we will print a statement that tells us that server is running smoothly.

app.listen(3000, function(req, res){
 console.log("server is running successfully");
});

After the above step check in the cmd terminal whether expserver.js is running or not, by typing the below command in the terminal.

E:\expressjswork>node expserver.js

The following screenshot will be flashed in the terminal.

Handling Request and Response (Get Request)

In this step, we will handle the request and response for our website/application so that whenever a user accesses our host-no. Or port-no. He would send a response from the server to the user with a message.

Also, the get request function will be used for which we will give a location for getting a request.

Type the code below for achieving the same:

app.get("/", function(req, res){  res.send("welcome to technical rider sam :)") });

Screenshot of the result after saving the above code given below.

Installing Nodemon

Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

To install Nodemon, type the below-given command in the terminal and run it.

E:\expressjswork>npm install -g nodemon