In express.js routing, we are going to learn how based on different urls we will are going to server different pages and content on the webpages.

Topics Covered 

  • Home Route – Opening home page on server
  • HTML File Route – Open HTML page on server
  • json Route – Open json response on the route
  • File Route – Send a complete file on the route section

Home Route –¬†Opening home page on server

var express = require("express");
var app = express();

app.get('/', function(req, res){
console.log("We are at home page");
res
    .status(200)
    .send("We are at Home Page of Pabbly");
});

var server = app.listen(3000, function() {
    console.log('App listening on port 3000!');
});

HTML File Route – Open HTML page response on the route

var express = require("express");
var app = express();
var path = require("path");

app.get('/htmlfile', function(req, res){
    console.log("We are at home page");
    res
        .status(200)
        .sendFile(path.join(__dirname, 'public', 'index.html'));
    });

var server = app.listen(3000, function() {
    console.log('App listening on port 3000!');
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>This is html Page</h1>
</body>
</html>

Json Route – Open json response on the route

var express = require("express");
var app = express();

app.get('/json', function(req, res){
    console.log("We are using Json Data");
    res
        .status(200)
        .json({"jsonData" : true});
    });

var server = app.listen(3000, function() {
    console.log('App listening on port 3000!');
});

File Route – Send a complete file on the route section

var express = require("express");
var app = express();

app.get('/file', function(req, res){
    console.log("We are going to open html file")
    res
        .status(200)
        .sendFile(path.join(__dirname, 'routing.js'));
    });

var server = app.listen(3000, function() {
    console.log('App listening on port 3000!');
});

Complete Code

routing.js

var express = require("express");
var app = express();
var path = require("path");

app.get('/', function(req, res){
console.log("We are at home page");
res
    .status(200)
    .send("We are at Home Page of Pabbly");
});

app.get('/htmlfile', function(req, res){
    console.log("We are at home page");
    res
        .status(200)
        .sendFile(path.join(__dirname, 'public', 'index.html'));
    });

app.get('/json', function(req, res){
console.log("We are using Json Data");
res
    .status(200)
    .json({"jsonData" : true});
});

app.get('/file', function(req, res){
console.log("We are going to open html file")
res
    .status(200)
    .sendFile(path.join(__dirname, 'routing.js'));
});

var server = app.listen(3000, function() {
    console.log('App listening on port 3000!');
});

Get More Knowledge: