Express JS

 

What is Express JS ?

ExpressJS is a fast and flexible web framework for NodeJS. It provides advance features for both web and mobile applications. It is an open-source framework developed and maintained by NodeJS foundation. ExpressJS simple API to build websites, web apps and backends.

Why ExpressJS is flexible ?

There are numerous modules available on npm, which can be directly plugged into express. So that ExpressJS is a flexible framework

MongoDB and Mongoose

  • MongoDB is an open-source document database.
  • Mongoose is a client API for NodeJS which makes it easy to access the database via ExpressJS.

How to start developing with ExpressJS ?

To start, you should have Node and npm (node package manager) installed in the PC.  npm is the package manager for Node. The npm registry is a public collection of packages with open source code for NodeJS frontend web apps, mobile apps, etc. npm allows to access those packages and install them locally. You can browse through the list of packages available on npm at npmJS.

There are two ways of installing packages using npm.

               1.      Globally – This method is used to install development tools and CLI based packages. Below command can be used to install packages globally.

                      npm install -g <package-name> 

                2.      Locally – This method is used to install frameworks and libraries. Locally installed packages can be used only within the directory it is installed. To install packages the locally,  the same command as above can be used without “ -g

                      npm install <package-name>

 Let us develop the simple project where we can experience about ExpressJS.

Step 1 – Open the command prompt(cmd) in the location where you would like to create the project. Create the project folder using the below command.

mkdir hello-world

Then go inside that project folder using the below command.

cd hello-world

Step 2 – Create the package.json file which provides all the information about our project. Give the below command in the cmd.

npm init



When you give the command “npm init” you may have to enter some details. You can leave them as default by pressing “Enter”. (If you want you can change the name of the “entry point” and you can give the your name to “author”)

Step 3 – As you have package.json file, you can use the below command to install ExpressJS.

       npm install --save express 

The “--save” flag ensures that the following dependency is saved in the package.json file. The main advantage of that is, when we want to start the project again it is enough to give the command “npm install” . So that command will install all the dependencies mentioned in the package.json.

But still, we may always need to restart the server manually when ever we make changes in any file. To avoid it, use the below command to install “nodemon”. It is a tool from npm which will automatically refresh the server what ever the changes we make in the files.

       npm install -g nodemon 

Step 4 – Create a new file called index.js and type the below code in it.

var express = require('express');

var app = express();

 

app.get('/', function(req, res){

   res.send("Hello world!");

});

 

app.listen(3000);

 Save the file and go to command prompt and type the below command to run the project.

       nodemon index.js 

To test this app, open your browser and go to  http://localhost:3000 and a message will be displayed as in the following screenshot.



How the process goes?

First line imports express to the file and you can access express from the variable “express”. We use it to create an application and assign it to var app.

app.get(route , callback)

This function tells what to do when the get request at the given route is called. The callback function has two parameters called, request(req) and response(res).

res.send()

This function takes an object as input and sends it to the requesting client.

app.listen(port , [host] , [backlog] , [callback])

This function binds and listens for connections on the specified host and port. Port  is the only required parameter here.

  • port – Server should accept the incoming requests via this port.
  • host – Name of the domain. You need to set this when you deploy apps in the cloud.
  • backlog - The maximum number of queued pending connections. The default is 511.
  • callback – An asynchronous function that is called when the server starts listening to the requests.

Advantages of ExpressJS

  • Make NodeJS development fast and easy.
  • Easy to configure and customize.
  • Allows to create a REST API server.
  • Easy to connect with databases such as MongoDB, Redis, MySQL.


Comments