ExpressJS doesn't fires next routes in app.use

眉间皱痕 提交于 2020-01-14 02:35:08

问题


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.


回答1:


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;
};


来源:https://stackoverflow.com/questions/57340270/expressjs-doesnt-fires-next-routes-in-app-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!