i

Express.js

Express.js is among most Popular frameworks creating a web application in Node.js. Using Express we can handle requests, create views and manage routes.

Express simplifies the application development for Node.js developers by introducing a set of APIs which extend Node.js and are easy to use. 

Advantages of using Express.js

  • Writing request handlers for different URLs in a straight forward manner.

  • We can use templating engines for view generation.

  • Using the Middleware concept, we can remove cross-cutting concerns like Logging, Authentication etc.

To develop an Express application, a development environment with below packages will be needed

1. Node.js
you can check it using
node –v

2. Express package installation

Using

npm I express

In order to implement an express application:

Steps to Follow

1. Setup Express development environment

2. Create a basic server using Express

3. Implement Routing functionality(optional)

4. Generate Views according to templating engine (optional)

5. Connect to the database (if data CRUD operation are to be performed)

6. Use Middleware in the application (optional; if data is to be )

7. Manage session and security features in the application.

Before making an express application, we need to install the Express module from npm library.

Using

>Npm I express

Make sure you have node and npm already installed on your device before installing Express..

Lets look an example for Express Demo

Express-demo.js

From the above code snippet, kets discuss a few concepts

Routing

To explore various features of a web application, we would need to request different URLs for a particular feature. For a particular web application, all the URLs will reach to the same server, but paths will be different. It is the server's responsibility to route the URLs to the appropriate features depending on the path.

In the above code snippet, we have a server created at https://localhost:3000/

but to access different functionalities, we would need different routes

https://localhost:3000/books for returning books collection stored.

https://localhost:3000/ for taking the user to home page URL.

Home.html

Output

                ‘’localhost:3000/

 

On checking MongoDB database

‘localhost:3000/books’

How can we send data from Client to Server

We can send the data in the http server in multiple ways using Express Framework.

  1. Inside Request body

  2. Inside Header

  3. Inside Query Parameters

  4. Send it inside resource parameters