What is Routing?
Routing defines the way in which the client requests are handled by the application endpoints.
Implementation of routing in Node.js: There are two ways to implement routing in node.js which are listed below:
- By Using Framework
- Without using Framework
Using Framework: Node has many frameworks to help you to get your server up and running. The most popular is Express.js.
Routing with Express in Node: 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.
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')
})
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');
Now create a server by adding the following lines of code:
http.createServer(function (req, res) {
res.write('Hello World!'); // Write a response
res.end(); // End the response
}).listen(3000, function() {
console.log("server start at port 3000"); // The server object listens on port 3000
});
Now add the following lines of code in the above function to perform routing:
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();
}
Example 1: The complete code of routing by combining the above code.
javascript
const http = require( 'http' );
http.createServer( function (req, res) {
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 () {
console.log("server start at port 3000");
});
|
Output:
Console Output

Browser Output

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!