简体   繁体   English

Express.js 在单个导出的路由器中返回多个路由 object 返回不正确的路由

[英]Express.js returning multiple routes in a single exported router object returns incorrect route

I am trying to utilize an MVC pattern for express.我正在尝试使用 MVC 模式进行表达。 I am modularizing routes and trying to declare the express router in only the server entry file and nowhere else.我正在模块化路由并尝试仅在服务器条目文件中声明快速路由器,而在其他任何地方都没有。

My current issue is when exporting my appRoute function in my main routes file (see below), the first route (user route), returns users.我当前的问题是在我的主路由文件(见下文)中导出我的appRoute function 时,第一条路由(用户路由)返回用户。 I have another route called game that is exported in the same function but it still returns users instead of games.我有另一条名为游戏的路线,它在同一个 function 中导出,但它仍然返回用户而不是游戏。

Both routes have a controller function called getAll that gets different data from different tables.两条路线都有一个名为 getAll 的 controller function 从不同的表中获取不同的数据。

If I try and visit the route: http://localhost:8000/user/getAll, it returns all users just fine.如果我尝试访问路线:http://localhost:8000/user/getAll,它会返回所有用户就好了。 If I try and visit the route: http://localhost:8000/game/getAll, it still returns all users even when they're different routes...如果我尝试访问路线:http://localhost:8000/game/getAll,即使他们是不同的路线,它仍然会返回所有用户......

If I were to flip the order of users and games in the main routes file where game is first and user is second, users starts to return games.如果我在主路由文件中将用户和游戏的顺序颠倒过来,游戏在前,用户在后,用户开始返回游戏。 It's like the second route mimics the first route.就像第二条路线模仿第一条路线一样。

This may be something simple, but any help I will appreciate.这可能很简单,但我将不胜感激。

My code is as shown below我的代码如下所示

server entry point (index.js)服务器入口点 (index.js)

const app = express();
const router = express.Router();
const bootstrap = require('./src/bootstrap');
bootstrap(app, router);

I am passing on the app and router instance to my bootstrap file where all routes will be exported to.我将应用程序和路由器实例传递给我的引导文件,所有路由都将导出到该文件。

bootstrap.js (this file gets all exported routes and uses them within the app) bootstrap.js (此文件获取所有导出的路由并在应用程序中使用它们)

const { appRoute } = require('./routes');

module.exports = (app, router) => {
  return app.use('/', appRoute(router));
};

I am passing on the router instance to my main routes file where all routes are exported from.我将路由器实例传递给我的主路由文件,所有路由都从该文件中导出。

Main routes file (index.js): this file requires all routes and uses them within the router instance.主路由文件(index.js):此文件需要所有路由并在路由器实例中使用它们。 I think this might be where my issue is but I am a little stuck on how I might fix it.我认为这可能是我的问题所在,但我对如何解决它有点困惑。

const { userRoute } = require('./userRoute');
const { gameRoute } = require('./gameRoute');

exports.appRoute = (router) => {
  router.use('/user', userRoute(router));
  router.use('/game', gameRoute(router));
  return router;
};

Game route files (index.js): returns all users instead of games游戏路由文件(index.js):返回所有用户而不是游戏

const { gameController } = require('../../controllers');

exports.gameRoute = (router) => {
  return router.get('/getAll', gameController.getAll);
};

Any help is greatly appreciated.任何帮助是极大的赞赏。 If there is any clarification needed please let me know.如果需要任何澄清,请告诉我。

I think you need to create a separate router for game and user.我认为您需要为游戏和用户创建一个单独的路由器。

See the express.Routing section and birds.js example here请参阅此处的 express.Routing 部分和birds.js 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM