Open In App

Routing in Node

Improve
Improve
Like Article
Like
Save
Share
Report

What is Routing in Node ?

Routing defines how the client requests are handled by the application endpoints. Routing in Node refers to the process of determining how an application responds to client requests to different endpoints (URLs). In a web application, these endpoints typically correspond to different pages or functionalities within the application. Node.js, along with frameworks like Express, provides a way to define routes, which are responsible for handling specific HTTP requests and sending back appropriate responses. 

There are two ways to implement routing in Node which are listed below:

Routing without using Express Framework:

For handling all HTTP methods (i.e. GET, POST, PUT, DELETE, etc.) use the app.all() method:

const express = require('express')
const app = express()
app.all('/', function(req, res) {
console.log('Hello Sir')
next() // Pass the control to the next handler
})

The next() is used to hand off the control to the next callback. Sometimes we use app.use() to specify the middleware function as the callback. So, to perform routing with Express.js you have only to load the express and then use the app object to handle the callbacks according to the requirement. Routing without Framework: Using the frameworks is good to save time, but sometimes this may not suit the situation. So, a developer may need to build up their own server without other dependencies. Now create a file with any name using .js extension and follow the steps to perform routing from scratch:

Here we will use the built-in module of node.js i.e. HTTP. So, First load http:

const http = require('http');

Example: The below code example is without using express framework

Javascript




const http = require('http');
 
// Create a server object
http.createServer(function (req, res) {
 
    // http header
    res.writeHead(200, { 'Content-Type': 'text/html' });
 
    const url = req.url;
 
    if (url === '/about') {
        res.write(' Welcome to about us page');
        res.end();
    }
    else if (url === '/contact') {
        res.write(' Welcome to contact us page');
        res.end();
    }
    else {
        res.write('Hello World!');
        res.end();
    }
}).listen(3000, function () {
    // The server object listens on port 3000
    console.log("server start at port 3000");
});


Console Output:

Browser Output:

Routing Using Express Framework:

Express.js has an “app” object corresponding to HTTP. We define the routes by using the methods of this “app” object. This app object specifies a callback function called when a request is received. We have different methods of in-app objects for different types of requests. Node has many frameworks to help you to get your server up and running. The most popular is Express.js. 

For a GET request using the app.get() method:

const express = require('express')
const app = express()

app.get('/', function(req, res) {
res.send('Hello Sir')
})

For POST requests use the app.post() method:

const express = require('express')
const app = express()

app.post('/', function(req, res) {
res.send('Hello Sir')
})

Example: The below code example is using express framework

Javascript




const express = require('express');
const app = express();
const port = 3000;
 
app.get('/', (req, res) => {
    res.send('Hello World!');
});
 
app.get('/about', (req, res) => {
    res.send('Welcome to about us page');
});
 
app.get('/contact', (req, res) => {
    res.send('Welcome to contact us page');
});
 
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});


Console Output:

Browser Output:



Last Updated : 15 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads