Javascript 安装api路由时运行函数

Javascript 安装api路由时运行函数,javascript,node.js,express,routing,Javascript,Node.js,Express,Routing,我正在为我的应用程序使用express router。我需要在安装特定路由时运行一个函数。我有两个文件,index.route.js和currentUser.route.js index.route.js import currentUserRoutes from './currentUser.route'; const router = express.Router(); //mounts the '/me' route on currentUserRoutes router.use('/m

我正在为我的应用程序使用express router。我需要在安装特定路由时运行一个函数。我有两个文件,
index.route.js
currentUser.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;
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;
currentUser.route.js

import currentUserRoutes from './currentUser.route';
const router = express.Router(); 
//mounts the '/me' route on currentUserRoutes
router.use('/me', currentUserRoutes);
export default router;
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.on('mount' , function(){console.log("me route mounted")});

我确实试过了,同样的issueconst router=require(express).router();执行此操作我正在导入文件顶部的express我可以使用
router.all('/*',currentUserCtrl.load)运行所需的函数