Kurs Google
Kurs Google

Reputation: 309

ExpressJS doesn't fires next routes in app.use

I have a problem that ExpressJS fires wrong routes while using app.use This is the code from index.js file, where i combine all routes:

const app = express(),
  Router = express.Router();

app.use("/api/vehicle", vehiclesRoutes(Router));
app.use("/api/static-costs", staticCostsRoutes(Router));
app.use("/api/company", companiesRoutes(Router));
app.use("/api/worker", workersRoutes(Router));

Each of this functions (vehiclesRoutes, staticCostsRoutes etc.) are constructed the same.

For example vehicleRoutes:

export default (router) => {
  router.get("/:company_id", getVehiclesByCompany);
  router.post("/:company_id", postVehicle);
  router.put("/:company_id/:id", putVehicle);
  router.delete("/:company_id/:id", deleteVehicle);
  return router;
};

For example staticCostsRoutes:

export default router => {
  router.get("/:company_id", getStaticCostsByCompany);
  router.post("/:company_id", postStaticCost);
  router.put("/:company_id/:id", putStaticCost);
  router.delete("/:company_id/:id", deleteStaticCost);
  return router;
};

I am passing Express Router, pinning routes and return Router.

When i am trying to call PUT /api/static-cost/:company_id/:id, it fires PUT /api/vehicles/:company_id/:id.

I mentioned something interesting, when i exchange these two things:

app.use("/api/static-costs", staticCostsRoutes(Router));
app.use("/api/vehicle", vehiclesRoutes(Router));

The situation is reverse, when i am trying to call PUT /api/vehicle/:company_id/:id,it fires PUT /api/static-costs/:company_id/:id.

Upvotes: 2

Views: 83

Answers (1)

Ramaraja
Ramaraja

Reputation: 2626

Problem

You are creating the Router object and passing the same reference to all the methods. If you closely observe in all the methods the routes are the same, only the controllers differ.

So, in this case, the commonly referenced Router passed to vehicleRoutes(Router) function registers the getVehiclesByCompany controller to the GET /:company_id route. And then the following attempts to register getStaticCostsByCompany controller at the same route in staticCostsRoutes(Router) function is ignored.

Solution

Instead of creating Router and passing the same reference to all the functions. Create a new Router object within each of the functions and return it so that they don't share the same reference.

const app = express(),

app.use("/api/vehicle", vehiclesRoutes());
app.use("/api/static-costs", staticCostsRoutes());
app.use("/api/company", companiesRoutes());
app.use("/api/worker", workersRoutes());

vehicleRoutes.js

const router = express.Router();
export default () => {
  router.get("/:company_id", getVehiclesByCompany);
  router.post("/:company_id", postVehicle);
  router.put("/:company_id/:id", putVehicle);
  router.delete("/:company_id/:id", deleteVehicle);
  return router;
};

staticCostsRoutes.js

const router = express.Router();
export default () => {
  router.get("/:company_id", getStaticCostsByCompany);
  router.post("/:company_id", postStaticCost);
  router.put("/:company_id/:id", putStaticCost);
  router.delete("/:company_id/:id", deleteStaticCost);
  return router;
};

Upvotes: 2

Related Questions