
Lecture Script: Refactoring Route Handling Using module.exports
Hi everyone! I hope everyone is doing fine.My name is Julian, and today we’re going to clean up our code and make it more organized.”
Right now, our code works just fine. But imagine if we had to handle 100 different endpoints! It could get really hard to manage if everything was in one place.”
Think of it like moving into a new house and placing all your stuff—clothes, kitchen items , books, your gym equipments—into one room.
At first, it seems okay.
But when you need something, like a spoon or your shoes,
it gets tough to find in that mess.”
Our code is just like that.
If we keep everything in one file,
it becomes messy and hard to manage as it grows.
That’s why today we’ll learn how to organize our code so it stays clean and it’s easy to work with.”
Right now, we have all the routing logic inside app.js, but this can get messy as our project grows.”
Instead of putting all the routes in app.js, we’ll move them into a new file called routes.js. This will keep things tidy and easier to work with.”
Enroll in Sharpener NOW !
At Sharpener Tech, we don’t just teach, we transform careers! Our Pay After Placement model ensures ZERO financial risk you pay only when you land a high-paying job. With industry-driven Full Stack Development training, hands-on projects, and expert mentorship, we guarantee your success in the tech world. Don’t just dream BUILD your future with Sharpener Tech! Apply now and start your journey!
Step 1: Current Setup
Let’s start by looking at how our code is set up right now. Inside app.js, we have something like this:”
javascript
Copy code
const http = require(‘http’);
const server = http.createServer((req, res) => {
const url = req.url;
if (url === ‘/’) {
res.write(‘<html><head><title>Home</title></head><body><h1>Welcome to the Home Page</h1></body></html>’);
return res.end();
}
if (url === ‘/about’) {
res.write(‘<html><head><title>About</title></head><body><h1>About Us</h1></body></html>’);
return res.end();
}
});
server.listen(3000);
Here, we’re checking the URL and sending different responses for different routes. It works, but it can get messy if we add more routes. So, let’s move this routing logic to another file.”
Step 2: Creating routes.js
First, create a new file called routes.js. Inside this file, we will write the routing logic.”
In routes.js, add the following code:
const requestHandler = (req, res) => {
const url = req.url;
if (url === ‘/’) {
res.write(‘<html><head><title>Home</title></head><body><h1>Welcome to the Home Page</h1></body></html>’);
return res.end();
}
if (url === ‘/about’) {
res.write(‘<html><head><title>About</title></head><body><h1>About Us</h1></body></html>’);
return res.end();
}
};
module.exports = requestHandler;
Here, we moved our route handling to the routes.js file. We also used module.exports to make the requestHandler function available for other files.”
Step 3: Updating app.js
Now, let’s go back to app.js. Instead of writing the routes here, we will import the requestHandler from routes.js.”
Modify app.js like this:
javascript
Copy code
const http = require(‘http’);
// Import the request handler from routes.js
const routes = require(‘./routes’);
const server = http.createServer(routes);
server.listen(3000);
We imported the requestHandler using require(‘./routes’). This allows us to use the routing logic from routes.js. Now, our app.js only handles starting the server.”
Step 4: Why Is This Useful?
By doing this, we keep our code more organized. All the routing is in one file (routes.js), and starting the server is in another (app.js). This way, as the project grows, our code stays clean and easy to manage.”
Step 5: More on module.exports
You can also export more than just one function. For now, we’re only exporting the requestHandler function, but later on, you might export other functions or objects too.”
Conclusion
To sum up, we learned how to:
- Move routing logic to a separate file.
- Use module.exports to export a function.
- Import that function into app.js.
This makes our code modular and easier to work with.