[英]Express Route not firing when called
I have a route defined in my express app, and I have it inside a router that is imported into my app in the main file. 我有一个在我的快递应用程序中定义的路由,我将它放在一个路由器中,该路由器在主文件中导入我的应用程序。
I have my router installed like this in index.js
我在
index.js
安装了这样的路由器
const Routes = require('./routes');
app.use('/address', Routes.AddressRoutes);
in the routes folder, I have the address router exposed like this: 在routes文件夹中,我有这样的地址路由器暴露:
//./routes/index.js
const AddressRoutes = require('./address');
module.exports = Routes = {
AddressRoutes,
};
// ./routes/address.js
const router = require('express').Router();
const {Address} = require('./routeActions');
module.exports = () => {
router.post('/', Address.add);
return router;
};
// ./routes/routeActions/index.js
const Address = require('./user');
module.exports = {
Address,
};
// ./routes/routeActions/address.js
module.exports = Address = {
add: (req,res) => {....}
};
The issue is that I have a breakpoint in VS code inside the add()
function, but the only thing that get's hit is the definition of the route, and not the actual handler. 问题是我在
add()
函数中的VS代码中有一个断点,但唯一受到影响的是路由的定义,而不是实际的处理程序。 Am I making the modules too complicated? 我的模块太复杂了吗? How can I get the handler to be called so I can debug it?
如何调用处理程序以便我可以调试它?
Two options to fix. 两个选项来修复。
When adding the route definitions, run the function that is exported. 添加路径定义时,请运行导出的功能。
const Address = require('./address')();
Or when exporting the route definitions, simply export the router from the file. 或者在导出路径定义时,只需从文件中导出路由器即可。
module.exports = router;
not module.exports = () => {routeHandlers.... return router}
不是
module.exports = () => {routeHandlers.... return router}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.