Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1.64 KB

routes.md

File metadata and controls

45 lines (33 loc) · 1.64 KB

How Express Routes Work Together in app.js and employeeRoutes.js

Let's break down how these two lines work together:

// in employeeRoutes.js
router.get("/", getAllEmployees);

// in app.js
app.use("/api/employees", employeeRoutes);

The Flow of an API Request

  1. 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
  2. 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/"

Examples

When a GET request is made to http://localhost:5000/api/employees/:

  1. Express first matches the base path "/api/employees" in app.js
  2. The request is forwarded to employeeRoutes
  3. Inside employeeRoutes.js, the "/" route matches
  4. The getAllEmployees controller function is executed

When a POST request is made to http://localhost:5000/api/employees/

  1. Express first matches the base path "/api/employees" in app.js
  2. The request is forwarded to employeeRoutes
  3. Inside employeeRoutes.js, the "/" route matches
  4. 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