javascript NodeJS 和 Express - 分离我的控制器和模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19977126/
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
NodeJS and Express - separate my controllers and models
提问by sf89
I'm building my first Express app. It's a bit messy as my controllers and my models are all in the same place: the app.js file. Is there a way that I can separate those? Even artificially, by creating different files and then using some third party program to compile them into the app.js file.
我正在构建我的第一个 Express 应用程序。这有点乱,因为我的控制器和模型都在同一个地方:app.js 文件。有没有办法把它们分开?甚至人为地,通过创建不同的文件,然后使用一些第三方程序将它们编译成 app.js 文件。
回答by Jessica Thedoc
First of all, you need to create your controllers and model folders.
首先,您需要创建控制器和模型文件夹。
You can use a module called express-load which can be used to autoload models, routes, schemas, configs, controllers, object maps... etc...
您可以使用名为 express-load 的模块,该模块可用于自动加载模型、路由、模式、配置、控制器、对象映射...等...
in your main file, mine is called app.js you load them right before start the server code line.. it should look like
在你的主文件中,我的名为 app.js 你在启动服务器代码行之前加载它们......它应该看起来像
//requires....
var load = require('express-load');
//your code
load('models')
.then('controllers')
.then('routes')
.into(app);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express listening on port "+ app.get('port'));
});
module.exports = app;
Then, your view folder you can create folders to keep your code organized, then subfolders, I created a folder called home, and inside of it my indexview.
然后,您的视图文件夹可以创建文件夹以保持代码井井有条,然后是子文件夹,我创建了一个名为home的文件夹,并在其中创建了我的索引视图。
In my controllers folder I created a js file called home.js, and which will look for my index view:
在我的控制器文件夹中,我创建了一个名为 home.js 的 js 文件,它将查找我的索引视图:
module.exports = function(app){
var HomeController = {
index: function(req, res){
res.render('home/index');
}
};
return HomeController;
}
At last in your routes folder, you can set your application routes, each view needs to be specified in your controller. My file for routes its called home.js
最后在你的路由文件夹中,你可以设置你的应用程序路由,每个视图都需要在你的控制器中指定。我的路由文件名为 home.js
module.exports = function(app){
var home = app.controllers.home;
app.get('/', home.index);
}
回答by Rituparna Kashyap
What I generally do it is to write a module which contains all the routes definition and load it in app.js
e.g
我通常做的是编写一个包含所有路由定义的模块并将其加载到app.js
例如
require('./routes')(app);
My ./routes.js
generally looks like this
我的./routes.js
一般看起来像这样
module.exports = function (app) {
log.info('Loading express routes...');
/* registration */
app.post('/users', require('./routes/register-users')); // register user
app.post('/agents', require('./routes/register-agents')); // register agents
};
and I keep all the routes (.js) files inside a directory call routes
我将所有路由 (.js) 文件保存在目录调用中 routes
Hope it is what you are looking for.
希望这是你正在寻找的。
回答by Hector Correa
Is there a way that I can separate those?
有没有办法把它们分开?
Yes, and you should separate them.
是的,您应该将它们分开。
What most people do is declare the routes in the main app.js file and include separate files for the controllers (just like Rituparna described).
大多数人所做的是在主 app.js 文件中声明路由,并为控制器包含单独的文件(就像 Rituparna 描述的那样)。
Those controllers files will in turn very likely include your model files via a require. For example.
这些控制器文件很可能会通过 require 包含您的模型文件。例如。
In app.js
在 app.js 中
var blogRoutes = require('./routes/blogRoutes');
app.get('/api/blog/all', blogRoutes.all);
In routes\blogRoutes.js
在路由\blogRoutes.js
var model = require('../models/blogModel');
var all = function(req, res) {
// your controller logic
// and very likely calls to your model
var m = model.blog(whatever);
res.send(m.something());
};
module.exports = {
all: all
}
In models\blogModel.js
在模型\blogModel.js
var something = function() {
return "something";
};
module.exports = {
something: something
}
You can see a working version of this in this repo https://github.com/hectorcorrea/hectorcorrea.com
你可以在这个 repo https://github.com/hectorcorrea/hectorcorrea.com 中看到这个的工作版本
回答by alessioalex
You should checkout the examples from the Express Github repo, there are multiple ways to do this (based on personal preference):
您应该查看 Express Github 存储库中的示例,有多种方法可以做到这一点(根据个人喜好):
https://github.com/visionmedia/express/tree/master/examples/mvchttps://github.com/visionmedia/express/blob/master/examples/resource/app.jshttps://github.com/visionmedia/express/tree/master/examples/route-separation
https://github.com/visionmedia/express/tree/master/examples/mvc https://github.com/visionmedia/express/blob/master/examples/resource/app.js https://github.com/ visionmedia/express/tree/master/examples/route-separation
回答by Zim
There are some examples here that may help you..
这里有一些例子可以帮助你..
Route Separation: https://github.com/visionmedia/express/tree/master/examples/route-separation
路由分离:https: //github.com/visionmedia/express/tree/master/examples/route-separation
MVP: https://github.com/visionmedia/express/tree/master/examples/mvc
MVP:https: //github.com/visionmedia/express/tree/master/examples/mvc