简体   繁体   English

如何在我的路由索引文件中使用外部路由

[英]How do I use external routes in my routes index file

I have a users.js and a index.js file.我有一个users.js和一个index.js文件。

users.js用户.js

const express = require('express');
const router = express.Router();
const {catchErrors} = require('../handlers/errorHandlers');

const authController = require('../controllers/authController');

router.post('login/', catchErrors(authController.login));

module.exports.router = router;

index.js索引.js

// Route handlers
let userRouter = require('./users');

router.use('/user', userRouter);

module.exports = router;

I tried this and it's not working.我试过这个,但它不起作用。 I would appreciate any advice.我将不胜感激任何建议。

In your users.js file you're exporting an object with the property router ( module.exports.router = router ) which would look like..在您的users.js文件中,您正在导出一个带有属性routermodule.exports.router = router )的对象,它看起来像..

module.exports = {
  router: router
}

and in your index you're importing the object from users.js but not accessing the router when passing it to router.use(...) .并且在您的索引中,您从users.js导入对象,但在将其传递给router.use(...)时不访问路由器。

You should pass the router in your index.js file您应该在index.js文件中传递路由器

index.js索引.js

const express = require('express');
const router = express.Router();

// You can access the router on the require
const userRouter = require('./users').router;
router.use('/user', userRouter);

// Or on the object after the require
const userRouter = require('./users');
router.use('/user', userRouter.router);

暂无
暂无

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

相关问题 我如何使用外部路由.js所以我不必在app.js中定义我的路由? - How do I use external routes.js so I don't have to define my routes in app.js? 如何在给定的快递文件中分隔路线? - How do I separate routes in my given express file? 如何将数据从一个 app.js 文件发送到 Routes 中的 index.ejs 文件 - how do I send data from one app.js file to index.ejs file in Routes 如何在节点应用程序的客户端的我的angularJS文件中引用Javascript路由文件? - How do I reference a Javascript routes file in my angularJS file on the client side in a node app? 如何在没有nodejs中所有路由的一个长文件的情况下进行路由? - How can I do my routing without having one long file of all the routes in nodejs? 如何将我的 html 代码连接到 javascript 中的控制器和路由? - How do I connect my html code to controllers and routes in javascript? 我如何使用角js路由为节点js应用程序 - how do i use angular js routes for node js application 如何将value变量从javascript文件夹中的普通js文件转换为nodejs中的route / index.js文件并进行表达? - How do i take the value variable from a normal js file in the javascript folder to the routes/index.js file in nodejs and express? 如何在路由器的路由中使用 .js 文件组件 - How to use .js file components in routes for a router 我的路由 index.js 文件出错(找不到模块) - My routes index.js file gives error (Module not found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM