Separating routes into files
📖 Scenario: You are building a simple Express web server. To keep your code clean and organized, you want to separate your routes into different files instead of putting all routes in one file.
🎯 Goal: Create an Express app that uses a separate route file for handling requests to /users. The main app file should import and use this route file.
📋 What You'll Learn
Create a route file named
users.js that exports an Express router.In
users.js, define a GET route for / that sends the text 'User list'.In the main app file
app.js, import the users.js router.Use the imported router in
app.js for the path /users.💡 Why This Matters
🌍 Real World
Separating routes into files helps keep your server code organized and easier to maintain as your app grows.
💼 Career
Many professional Express apps use routers in separate files to manage complexity and collaborate with teams.
Progress0 / 4 steps