Let's break down how these two lines work together:
// in employeeRoutes.js
router.get("/", getAllEmployees);
// in app.js
app.use("/api/employees", employeeRoutes);
-
Base URL Registration (app.js)
app.use("/api/employees", employeeRoutes)
registers the employeeRoutes module as middleware- It sets "/api/employees" as the base path for all routes defined in employeeRoutes.js
- Any request that starts with "/api/employees" will be forwarded to the employeeRoutes handler
-
Route Handler (employeeRoutes.js)
router.get("/", getAllEmployees)
defines a GET endpoint- The "/" path here is relative to the base path defined in app.js
- When combined with the base path, this creates the full route: "/api/employees/"
When a GET request is made to http://localhost:5000/api/employees/
:
- Express first matches the base path "/api/employees" in app.js
- The request is forwarded to employeeRoutes
- Inside employeeRoutes.js, the "/" route matches
- The getAllEmployees controller function is executed
When a POST request is made to http://localhost:5000/api/employees/
- Express first matches the base path "/api/employees" in app.js
- The request is forwarded to employeeRoutes
- Inside employeeRoutes.js, the "/" route matches
- The createEmployee controller function is executed
This modular approach allows for:
- Clean separation of routes into different files
- Logical grouping of related endpoints
- Easy maintenance and scaling of the API
- Consistent URL structure with common prefixes