ExpressJS | Understanding of Expressjs Routing

Reading Time: 2 minutes

This article will learn about Expressjs, Installation & Routing in Express.js. Let’s start with Expressjs Introduction.

What is ExpressJS?

ExpressJS is a fast, unopinionated, minimalist web framework for Node.js. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Environment & Installation:

Before installing expressjs, install nodejs. Nodesjs version should be 0.10 or higher. To check the Nodejs version, use the below-mentioned command:

$ node -version

First, we need to create a package.json file that will hold the project dependencies. To create package.json file use the below command:

$ npm init

Now, to install Expressjs globally use the below command:

$ npm install -g express

Or, to install it locally using the command:

$ npm install express –save

Let’s create a simple code to test Expressjs Framework:

Expressjs Routing:

Expressjs routing specifies how an application responds to a client request to a particular route, URI, or path and a specific HTTP request method (GET, POST, etc.).

In straightforward terms, routing is controlling which function gets invoked whenever the client explores a specific URL.

Syntax: app.METHOD(PATH, HANDLER)

where an app is an instance of express. We can use any variables. This function tells the server, “If client navigates to PATH, then perform the following CALLBACK function and perform an HTTP METHOD request.”

Route Method:

A route method is derived from one of the HTTP methods and is attached to an instance of the express class. The most commonly used HTTP methods are:

1. GET Method: To handle GET requests (i.e. to request/GET data from a specified resource).

2. POST Method: To send data to a server to create a resource.

3. PUT Method: To send data to a server to update a resource.

4. DELETE Method: To delete a specific resource.

Route Path:

PATH is the route to the server for a specific webpage

Route Handler:

HANDLER is the callback function that is executed when the matching route is found.

Here is an example defining simple routes:

Conclusion

So, With this blog, I hope I was able to explain that what is Expressjs, the installation process, and express basic routing.

Thank you for reading, if you really like the blog hit the like button, and to learn more, Visit here