亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

快速嵌套路由器不調用子功能

快速嵌套路由器不調用子功能

牛魔王的故事 2021-06-30 09:11:17
我的其中一臺路由器中有以下 router.use 調用router.use("/:collection/", (req) => {    return require(`./${req.params.collection}`);});在這個例子中調用,example.jsexample.js 如下:const header = require("../../header"); //gets our header that declares everythingconst router = header.express.Router(); //makes our router for collections requestsconsole.log("123");///The Following is when a name is requestedrouter.get("/test", (req, res, next) => {    console.log("test");    res.json({msg:"hi"});    next();});module.exports = router; //makes our router avialable你會期望什么時候:http://localhost:3000/api/example/test請求它會在控制臺中寫入以下內容:123test我會得到回應:{msg:"hi"}相反,控制臺只得到:123寫了,沒有回應。看來 router.get在 example.js 中從未被調用,有人能告訴我為什么嗎?
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

我修復了它,而不是


router.use("/:collection/", (req) => {

    return require(`./${req.params.collection}`);

});


我用


router.get("/:collection", (req, res) => {

  //this is my other call that will do stuff in the parent file

  //we don't call next because it is already matched, otherwise we call next

});


router.use("/:collection/", (req, res, next) =>{ //says if it gets here pass on the info

  router.use("/:collection/", require(`./${req.params.collection}`)); //then route

  next();

});


查看完整回答
反對 回復 2021-07-08
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號