简体   繁体   English

在express.Router();中存储路由

[英]Storing Routes in express.Router();

I am currently working on the backend for a little project in angular and express. 我目前正在为有关angular和express的小项目做后端工作。 Since there will be a lot of Routes, I decided to create a file for each route-block. 由于会有很多路由,所以我决定为每个路由块创建一个文件。 After that, I am importing them into my server.js like this: 之后,我将它们导入到我的server.js中,如下所示:

router = express.Router();
var routes = require('./routepath/route1');
routes(router);
routes = require('./routepath/route2');
routes(router);
routes = require('./routepath/route3');
routes(router);

Is there an easier way to do this? 有没有更简单的方法可以做到这一点? I don't want to call routes(router) for each routefile i create. 我不想为我创建的每个routefile调用route(router)。

You can see https://github.com/praveen001/express-boilerplate/tree/master/src for a little cleaner example. 您可以看到https://github.com/praveen001/express-boilerplate/tree/master/src ,这是一个更简洁的示例。

But you still have to import every route and add it manually, there is no escaping from it, as far as I know. 但是,据我所知,您仍然必须导入每条路线并手动添加,没有逃脱的可能。

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

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