Express Routing

Earlier we have seen GET and POST methods which are used to build RESTfull API’s. Apart from that we also do have few more methods which are used in building API.

Routing is used to define the response for an endpoint or URI.

Below is an example for routing.
var express = require('express');  
var app = express();  
app.get('/', function (req, res) {  
   console.log("Got a GET request for the homepage");  
   res.send('Welcome to salesforce drillers');  
})'/', function (req, res) {  
   console.log("Got a POST request for the homepage");  
   res.send('Post request');  
app.delete('/del_student', function (req, res) {  
   console.log("Got a DELETE request for /del_student");  
   res.send('You have deleted');  
app.get('/enrolled_student', function (req, res) {  
   console.log("Got a GET request for /enrolled_student");  
   res.send('Get request');  
// 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('Pattern Matched.');  
var server = app.listen(8000, function () {  
var host = server.address().address  
  var port = server.address().port  
console.log("Example app listening at http://%s:%s", host, port)  

We have already seen how it works in earlier sections.

Subscribe Now