【问题标题】:Run a function when an api route is mounted挂载 api 路由时运行函数
【发布时间】:2017-01-04 11:43:32
【问题描述】:

我正在为我的应用程序使用快速路由器。安装特定路线时,我需要运行一个功能。我有两个文件,index.route.jscurrentUser.route.js

index.route.js

import currentUserRoutes from './currentUser.route';
const router = express.Router(); 
//mounts the '/me' route on currentUserRoutes
router.use('/me', currentUserRoutes);
export default router;

currentUser.route.js

const router = express.Router();
//line below throws an error
router.on('mount' , () => {console.log("me route mounted")});

router.route('/')
    .get(currentUserCtrl.get)

export default router;

它会抛出一个错误,说 router.on 不是函数,我也尝试将 router.on 部分放在 index.route.js 中,但我也遇到了同样的错误。任何帮助将不胜感激。

【问题讨论】:

  • 我能够使用router.all('/*', currentUserCtrl.load); 运行我需要的功能。

标签: javascript node.js express routing


【解决方案1】:

试试这个:

router.on('mount' , function(){console.log("me route mounted")});

【讨论】:

  • 我试过了,同样的问题
  • const router = require(express).Router();这样做
  • 我在文件顶部导入 express
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多