ExpressJS不会在app.use中触发下一条路线 [英] ExpressJS doesn't fires next routes in app.use
问题描述
我有一个问题,即ExpressJS在使用app.use时会触发错误的路由 这是index.js文件中的代码,在这里我组合了所有路由:
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));
每个功能(车辆路线,staticCostsRoute等)的构造都相同.
Each of this functions (vehiclesRoutes, staticCostsRoutes etc.) are constructed the same.
例如车辆路线:
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;
};
例如staticCostsRoutes:
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;
};
我正在传递Express Router,固定路由并返回Router.
I am passing Express Router, pinning routes and return Router.
当我尝试致电 PUT/api/static-cost/:company_id/:id 时, 它会触发 PUT/api/vehicles/:company_id/:id .
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));
情况相反,当我尝试致电 PUT/api/vehicle/:company_id/:id 时,它触发 PUT/api/static-costs/:company_id/: ID .
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.
推荐答案
问题
您正在创建Router
对象,并将相同的引用传递给所有方法.
如果仔细观察所有方法的路由都是相同的,则只有控制器有所不同.
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.
因此,在这种情况下,传递给vehicleRoutes(Router)
函数的通常引用的Router
将getVehiclesByCompany
控制器注册到GET /:company_id
路由.然后,以下尝试在staticCostsRoutes(Router)
函数中的同一路径上注册getStaticCostsByCompany
控制器的尝试将被忽略.
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.
解决方案
而不是创建Router
并将相同的引用传递给所有函数.
在每个函数中创建一个新的Router
对象,然后将其返回,以使它们不会共享相同的引用.
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
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
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;
};
这篇关于ExpressJS不会在app.use中触发下一条路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!