0% found this document useful (0 votes)
43 views

NodeJS Routing

The document discusses routing in Node.js applications, both with and without a framework. Routing refers to how incoming HTTP requests are handled and which code generates the response. Without a framework, routing requires manually handling each request. Express.js is a popular framework that simplifies routing through defined routes and middleware. Key differences are that frameworks make routing more organized, intuitive, and support middleware integration and community support.

Uploaded by

Bhautik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

NodeJS Routing

The document discusses routing in Node.js applications, both with and without a framework. Routing refers to how incoming HTTP requests are handled and which code generates the response. Without a framework, routing requires manually handling each request. Express.js is a popular framework that simplifies routing through defined routes and middleware. Key differences are that frameworks make routing more organized, intuitive, and support middleware integration and community support.

Uploaded by

Bhautik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Routing in Node.

js
Routing in Node.js
• Routing in Node.js refers to the process of determining how an incoming HTTP
request should be handled and which code or function should be executed to
generate the appropriate response.
• Routing is a fundamental part of building web applications and APIs.
• Routing can be implemented using a web framework or without any framework.
Routing without framework
• Routing without a framework involves manually handling HTTP requests and writing
custom code to determine how each request should be handled.
Routing without framework
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome to the homepage!');
} else if (req.url === '/about') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('About Us Page');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page Not Found');
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Routing with framework - Express.js
• Using a web framework like Express.js makes routing much more organized and
efficient. Express simplifies the process of defining routes and handling HTTP
requests.
Routing with framework - Express.js
const express = require('express');
const app = express();
// Define routes
app.get('/', (req, res) => {
res.send('Welcome to the homepage!');
});
app.get('/about', (req, res) => {
res.send('About Us Page');
});
// Handle 404 errors
app.use((req, res) => {
res.status(404).send('Page Not Found');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Routing with & without framework - Key Difference
• Ease of Use: Using a framework like Express simplifies routing and makes it more
intuitive.

• Organized Code: Frameworks encourage a more organized and modular code


structure, making it easier to manage routes as your application grows.

• Middleware: Express allows you to use middleware functions for tasks like
authentication, logging, and error handling, which can be easily integrated into your
routing.

• Community Support: Frameworks have large communities with extensive


documentation and plugins, making it easier to find solutions to common problems.
?

You might also like