Example basic routing in express

We have seen a basic application which serves HTTP request for the homepage. Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Example:

 

var express = require('express');

var app = express();

// This responds with "Hello World" on the homepage

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

   console.log("Got a GET request for the homepage");

   res.send('Hello GET');

});

// This responds a POST request for the homepage

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

   console.log("Got a POST request for the homepage");

   res.send('Hello POST');

});

 

// This responds a DELETE request for the /del_user page.

app.delete('/del_user', function (req, res) {

   console.log("Got a DELETE request for /del_user");

   res.send('Hello DELETE');

});

 

// This responds a GET request for the /list_user page.

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

   console.log("Got a GET request for /list_user");

   res.send('Page Listing');

});

 

// This responds a GET request for abcd, abxcd, ab123cd, and so on

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

   console.log("Got a GET request for /ab*cd");

   res.send('Page Pattern Match');

});

 

var server = app.listen(8080, function () {

 

   var host = server.address().address;

   var port = server.address().port;

 

   console.log("Server listening at http://%s:%s", host, port);

});

 

Run server and check with Postman

GET: http://localhost:8080

POST: http://localhost:8080

DELETE: http://localhost:8080/del_user

GET: http://localhost:8080/list_user

GET: http://localhost:8080/abcodewrcd

Output:

 

C:\Nodejs>node express_basic_routing.js

Server listening at http://:::8080

Got a GET request for the homepage

Got a POST request for the homepage

Got a DELETE request for /del_user

Got a GET request for /list_user

Got a GET request for /ab*cd