Node.js + Express:路由与控制器 [英] Node.js + Express: Routes vs controller

查看:115
本文介绍了Node.js + Express:路由与控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经看到了应用程序简单的例子。 js + routes / *,这似乎足以路由所需的各种请求。

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

但是,我也看到有人在谈论使用控制器,有些则暗示着一个更正式的MVC模型(???)。

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

如果有人可以帮助我清除这个谜,那么很棒,如果你有一个很好的例子来设置Node.js + Express框架中的控制器,那将非常棒!

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!

谢谢,

推荐答案

Express(和Node一般)向你推荐很多意见;其中一个缺点是它不会对你发表任何意见。因此,您是自由的(和必需的)自己​​设置任何这样的意见(模式)。

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,您一定可以使用MVC模式和路由处理程序当然可以发挥控制器的作用,如果你愿意的话,但你必须这样设置。 Express示例文件夹中可以找到一个很好的例子,名为 MVC 。如果您查看 lib / boot.js ,您可以看到他们如何设置示例,要求控制器目录中的每个文件,并即时生成Express路由取决于在控制器上创建的方法的名称。

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 controllers directory, and generate the Express routes on the fly depending on the name of the methods created on the controllers.

这篇关于Node.js + Express:路由与控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆