What are Routes? Express FrameWork

What are Routes?

Routing determine the way in which an application responds to a client request to a particular endpoint.

For example, a client can make a GET, POST, PUT or DELETE http request for various URL such as the ones shown below;

http://localhost:3000/Books
http://localhost:3000/Students

In the above example,

  • If a GET request is made for the first URL, then the response should ideally be a list of books.
  • If the GET request is made for the second URL, then the response should ideally be a list of Students.
  • So based on the URL which is accessed, a different functionality on the webserver will be invoked, and accordingly, the response will be sent to the client. This is the concept of routing.

Each route can have one or more handler functions, which are executed when the route is matched.

The general syntax for a route is shown below

app.METHOD(PATH, HANDLER)

Wherein,

1) app is an instance of the express module

2) METHOD is an HTTP request method (GET, POST, PUT or DELETE)

3) PATH is a path on the server.

4) HANDLER is the function executed when the route is matched.

Let’s look at an example of how we can implement routes in the express. Our example will create 3 routes as

  1. A /Node route which will display the string “Tutorial on Node” if this route is accessed
  2. A /Angular route which will display the string “Tutorial on Angular” if this route is accessed
  3. A default route / which will display the string “Welcome to Guru99 Tutorials.”

Our basic code will remain the same as previous examples. The below snippet is an add-on to showcase how routing is implemented.

Node.js Express FrameWork Tutorial - Learn in 10 Minutes

var express = require('express');
var app = express();
app.route('/Node',get(function(req,res)
{
    res.send("Tutorial on Node");
});
app.route('/Angular',get(function(req,res)
{
    res.send("Tutorial on Angular");
});
app.get('/',(function(req,res){
    res.send('Welcome to Guru99 Tutorials');
}));

Code Explanation:

  1. Here we are defining a route if the URL http://localhost:3000/Node is selected in the browser. To the route, we are attaching a callback function which will be called when we browse to the Node URL.The function has 2 parameters.
  • The main parameter we will be using is the ‘res’ parameter, which can be used to send information back to the client.
  • The ‘req’ parameter has information about the request being made. Sometimes additional parameters could be sent as part of the request being made, and hence the ‘req’ parameter can be used to find the additional parameters being sent.
  1. We are using the send function to send the string “Tutorial on Node” back to the client if the Node route is chosen.
  2. Here we are defining a route if the URL http://localhost:3000/Angular is selected in the browser. To the route, we are attaching a callback function which will be called when we browse to the Angular URL.
  3. We are using the send function to send the string “Tutorial on Angular” back to the client if the Angular route is chosen.
  4. This is the default route which is chosen when one browses to the route of the application – http://localhost:3000. When the default route is chosen, the message “Welcome to Guru99 Tutorials” will be sent to the client.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email