最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS and Express - separate my controllers and models - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Nov 14, 2013 at 12:02 sf89sf89 5,2387 gold badges25 silver badges27 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 7

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...

in your main file, mine is called app.js you load them right before start the server code line.. it should look like

//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 index view.

In my controllers folder I created a js file called home.js, and which will look for my index view:

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

module.exports = function(app){
    var home = app.controllers.home;

    app.get('/', home.index);
}

What I generally do it is to write a module which contains all the routes definition and load it in app.js e.g

require('./routes')(app);

My ./routes.js generally looks like this

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

Hope it is what you are looking for.

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).

Those controllers files will in turn very likely include your model files via a require. For example.

In app.js

var blogRoutes = require('./routes/blogRoutes');
app.get('/api/blog/all', blogRoutes.all);

In routes\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

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

You should checkout the examples from the Express Github repo, there are multiple ways to do this (based on personal preference):

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

There are some examples here that may help you..

Route Separation: https://github.com/visionmedia/express/tree/master/examples/route-separation

MVP: https://github.com/visionmedia/express/tree/master/examples/mvc

发布评论

评论列表(0)

  1. 暂无评论