Node.js + Express:路由与控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11076179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:52:53  来源:igfitidea点击:

Node.js + Express: Routes vs controller

node.jsexpress

提问by user1462192

New to Node.js and Express, I am trying to understand the two seems overlapping concepts, routes vs controller.

Node.js 和 Express 的新手,我试图理解这两个似乎重叠的概念,路由与控制器。

I have seen examples that simple does app.js + routes/*, this seems to be sufficient to route various requests needed.

我见过简单的 app.js + routes/* 的例子,这似乎足以路由所需的各种请求。

However, I also see people talking about using controllers, and some that implies a more formal MVC model (???).

但是,我也看到人们在谈论使用控制器,其中一些暗示了更正式的 MVC 模型 (???)。

Would be great if someone can help me clear this mystery, and if you have a good example for setting up controller in Node.js + Express framework that will be great!

如果有人能帮我解开这个谜,那就太好了,如果你有一个在 Node.js + Express 框架中设置控制器的好例子,那就太好了!

Thanks,

谢谢,

回答by Michelle Tilley

One of the cool things about Express (and Node in general) is it doesn't push a lot of opinions on you; one of the downsides is it doesn't push any opinions on you. Thus, you are free (and required!) to set up any such opinions (patterns) on your own.

Express(以及一般的 Node)的一个很酷的事情是它不会把很多意见推给你;缺点之一是它不会对您提出任何意见。因此,您可以(并且必须!)自行设置任何此类意见(模式)。

In the case of Express, you can definitely use an MVC pattern, and a route handler can certainly serve the role of a controller if you so desire--but you have to set it up that way. A great example can be found in the Express examples folder, called mvc. If you look at lib/boot.js, you can see how they've set up the example to require each file in the controllersdirectory, and generate the Express routes on the fly depending on the name of the methods created on the controllers.

在 Express 的情况下,您绝对可以使用 MVC 模式,并且如果您愿意,路由处理程序当然可以充当控制器的角色——但您必须以这种方式设置它。在Express 示例文件夹中可以找到一个很好的示例,名为mvc. 如果您查看lib/boot.js,您可以看到他们如何设置示例以要求目录中的每个文件controllers,并根据在控制器上创建的方法的名称动态生成 Express 路由。

回答by C. Johari

You can just have a routes folder or both. For example, some set routes/paths (ex. /user/:id) and connect them to Get, Post, Put/Update, Delete, etc. and then in the routes folder:

您可以只有一个路由文件夹,或者两者都有。例如,一些设置路由/路径(例如 /user/:id)并将它们连接到 Get、Post、Put/Update、Delete 等,然后在路由文件夹中:

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

Router.use('/subs/:id);

Router
 .route('subs/:id');
 .get(
 subController.getSub
 .patch(
 subController.updateSub
 );

Then, in the controllers folder:

然后,在控制器文件夹中:

exports.getSub = (req, res, next) => {
  req.params.id = req.users.id;
};

Just to make something. I've done projects with no controllers folder, and placed all the logic in one place.

只是为了做点什么。我已经完成了没有控制器文件夹的项目,并将所有逻辑放在一个地方。