如何在没有nodejs中所有路由的一个长文件的情况下进行路由?

时间:2020-11-29 17:41:09

Using this link as a reference https://github.com/visionmedia/express/tree/master/examples/route-separation to what "could be done"

使用这个链接作为引用https://github.com/visionmedia/express/tree/master/examples/route-separation到“可以做什么”

I AM NOT USING EXPRESS. I AM USING THEM AS AN EXAMPLE.

我不是在用快递。我用它们作为一个例子。

I want to do something like this but "simpler" ...

我想做这样的事情,但“更简单”……

How can I get away from declaring all my routes in one long, complex list all in one file? Can I define them by passing a router into my modules, and then including all the code in one directory ... ok, I'll suffer having one long document that only does "require" includes, like an index.js, for this one ~ at least that one my build scripts can rebuild for me, but preferably not in my primary file for every single route that I may add.

如何避免在一个长长的、复杂的列表中声明所有的路由?我可以通过将路由器传入我的模块来定义它们,然后将所有代码包含在一个目录中……好吧,我将遭受一个只包含“require”的长文档的痛苦,比如一个索引。对于这个,至少我的构建脚本可以为我重新构建一个,但是最好不要在我的主文件中为我可能添加的每个路由重新构建一个。

So for instance, they use this code:

例如,他们使用这个代码

// General

app.get('/', site.index);

// User

app.all('/users', user.list);
app.all('/user/:id/:op?', user.load);
app.get('/user/:id', user.view);
app.get('/user/:id/view', user.view);
app.get('/user/:id/edit', user.edit);
app.put('/user/:id/edit', user.update);

// Posts

app.get('/posts', post.list);

I want to avoid making a list like that in my app.js. I want instead to have each file know what the routes are for that file.

我想避免在我的app.js中创建这样的列表。我想让每个文件知道这个文件的路径。

Here's what I'm wanting to do: (please don't critique the code, I'm making it very simple so I make sure that I am illustrating my code the way I want to do it)

这里是我想做的:(请不要批评代码,我把它做得非常简单,所以我要确保以我想要的方式来说明代码)

//app.js
var router = require('./myRouter.js')
var includes = require('./routes/*.js').register(router)

// do some stuff here with an https server and start the server here

and

//./routes/user.js
var myRouter;
exports.register(router){
  myRouter = router;
}
router.addRoute(/* here I do the magic associated with this route */)

Can I do it just that simply? What am I missing here?

我能这么简单地做吗?我错过了什么?

I haven't written this code because I'm just ever so certain that I'm going about this the wrong way.

我还没有写这段代码,因为我一直都很确定我会用错误的方式来写。

And if I am going to have to use something like an index.js in the /routes/ folder, can I use that same concept that I demonstrated I would like to use in my code of .register(router) appended so I can pass that information down recursively? Would that work?

如果我要用指数之类的东西。在/route /文件夹中的js中,我是否可以使用我在附加的.register(router)代码中演示过的概念,以便递归地传递这些信息?会工作吗?

2 个解决方案

#1


3  

I use an index.js file for this and use require("routes") which is a folder.

我使用一个索引。js文件的这个和使用要求(“路由”)是一个文件夹。

// app.js
route = require("./routes"),
...
route(app);

// routes/index.js
var index = require("./index-route"),
    about = require("./about-route"),
    posts = require("./posts-route");

module.exports = function(app) {
    index(app);
    about(app);
    posts(app);
};

This works because if you require a folder it will load index.js by default.

这之所以有效,是因为如果您需要一个文件夹,它将加载索引。js默认情况下。

If you have a lot of routes you might want to load them based on convention

如果您有许多路由,您可能希望根据约定加载它们

var routes = [];
// read all files
fs.readdir("./", function(files) {
    files.forEach(function(val) {
        // require all non-index.js files.
        if (val !== "index.js") {
            routes.push(require(val));
        }
    });
});

module.exports = function(app) {
    // for each route you required call it with app.
    routes.forEach(val.bind(null, app));
}

This would load all .js files that are not "index.js", so any file in your /routes/ folder would be loaded and run when you route them.

这将加载所有非“索引”的.js文件。因此,您/route /文件夹中的任何文件都将在路由时被加载并运行。

#2


1  

Your solution looks vaguely like you wish to use the Visitor Patern, in which case I suggest you make ./roots/ require-able (see this question) and in index.js you include all the files you wish (as local's) and export a register module which calls the register module on each of the required files.

您的解决方案看起来有点像您希望使用访问者Patern,在这种情况下,我建议您使用。/root / require-able(参见这个问题)和index。js包括你希望的所有文件(作为本地文件),并导出一个注册模块,该模块调用每个需要的文件上的寄存器模块。

Or you could copy the code from the above answer directly into your main file.

或者您可以将上面的答案中的代码直接复制到您的主文件中。

#1


3  

I use an index.js file for this and use require("routes") which is a folder.

我使用一个索引。js文件的这个和使用要求(“路由”)是一个文件夹。

// app.js
route = require("./routes"),
...
route(app);

// routes/index.js
var index = require("./index-route"),
    about = require("./about-route"),
    posts = require("./posts-route");

module.exports = function(app) {
    index(app);
    about(app);
    posts(app);
};

This works because if you require a folder it will load index.js by default.

这之所以有效,是因为如果您需要一个文件夹,它将加载索引。js默认情况下。

If you have a lot of routes you might want to load them based on convention

如果您有许多路由,您可能希望根据约定加载它们

var routes = [];
// read all files
fs.readdir("./", function(files) {
    files.forEach(function(val) {
        // require all non-index.js files.
        if (val !== "index.js") {
            routes.push(require(val));
        }
    });
});

module.exports = function(app) {
    // for each route you required call it with app.
    routes.forEach(val.bind(null, app));
}

This would load all .js files that are not "index.js", so any file in your /routes/ folder would be loaded and run when you route them.

这将加载所有非“索引”的.js文件。因此,您/route /文件夹中的任何文件都将在路由时被加载并运行。

#2


1  

Your solution looks vaguely like you wish to use the Visitor Patern, in which case I suggest you make ./roots/ require-able (see this question) and in index.js you include all the files you wish (as local's) and export a register module which calls the register module on each of the required files.

您的解决方案看起来有点像您希望使用访问者Patern,在这种情况下,我建议您使用。/root / require-able(参见这个问题)和index。js包括你希望的所有文件(作为本地文件),并导出一个注册模块,该模块调用每个需要的文件上的寄存器模块。

Or you could copy the code from the above answer directly into your main file.

或者您可以将上面的答案中的代码直接复制到您的主文件中。