22

Say i have this code to separate routes in expressjs:

module.exports = function(express,app,client) {

    app.get('/', function(req,res,next) {
        var query = 'SELECT * FROM users LIMIT 10';
        var user = client.query(query, function (err, results, fields) {
            res.render('index', {
                title: 'test',
                users: results
            });
            client.end();
        });
    });
}

And require it in app.js:

require('./controllers/routes.js')(express,app,client);

1) How do i separate db queries into new files in the best way?

This file would get pretty big even if i separate db logic.

2) What is a good way to separate routes? Maybe separate modules? and then require them all in app.js?

georgesamper
  • 4,989
  • 5
  • 40
  • 59

3 Answers3

33

There is a similar question here which you should read: How to structure a express.js application?

1) All your query logic should be put in models (modules that reside in /models for example)

2) Separate all your routes (controllers) into modules (and put them in /routes for ex) By routes I mean for example: - all the logic for "Users" routes go into /routes/users.js

Try to keep you app as MVC-ish as possible.

Small example for your app above:

app.js

// configuration for express etc
require('./routes/index')(app)

routes/index.js

var model = require("../models/users.js");

module.exports = function (app) {

  app.get('/', function (req, res, next) {
    model.get_recent(function (err, results) {
      // do stuff with your results
      res.render('index');
    });
  });

}

models/users.js

module.exports = {
  get_recent: function(callback) {
    var query = "SELECT * FROM users LIMIT 10";
    database.query(query, callback);
  }
}
Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • Ok. Yeah im trying to follow the mvc pattern, but having a hard time wrapping my mind around how to do this with expressjs. So i can do it the way i am doing it now? And require all routes in app.js? And the models, how do i separate it in the example above? Could you maybe show a small example given the code i provided? Would be much appreciated. – georgesamper Dec 08 '11 at 08:53
  • Well that makes perfect sense now, Thank you very much for clearing that out :) – georgesamper Dec 08 '11 at 09:10
  • In my opinion, having DB queries in the model is not very MVCish, but I guess there many ways to do it. – UpTheCreek Mar 23 '12 at 15:05
  • 3
    Shouldn't it be `require('./routes')(app);`? – ThomasReggi Aug 22 '12 at 14:10
  • @alessioalex Isn't this line `database.query(query, callback);` going to hang the node application? I was just coding something similar, but my approach is to use some events for this. But I am a bit new to node, not sure is I am getting all this right. – Eduárd Moldován Mar 05 '13 at 11:39
  • @EduárdMoldován in Node you can't "hang" the application, like you do in PHP for example with `sleep()`. In the meanwhile you can do other actions if you want (like multiple queries to the database, parallel processing etc). If by "hang" you mean wait for the callback to be executed though, then yes :-) – alessioalex Mar 06 '13 at 06:44
  • That is exactly what I meant by hang, the last one. – Eduárd Moldován Mar 06 '13 at 09:22
  • I was wondering how you passed "app" to the module... thank you!! – jlmakes Mar 28 '13 at 21:41
  • He did it like this: `require('./routes/index')(app)` – aliopi Aug 26 '15 at 13:22
5

In the expressjs download package, there is a folder called "mvc". The author provides a good example for a tiny&efficient mvc structure. Going through the code, you will get much inspiration.

Len Xu
  • 91
  • 1
  • 1
  • 2
    This can be found at the repo [**`visionmedia/express`**](https://github.com/visionmedia/express) at the directory [**`express/examples/mvc`**](https://github.com/visionmedia/express/tree/master/examples/mvc) for those that are as interested in this as I was. – Cory Gross May 13 '13 at 19:04
1

How about express-train ? i have been using it lately, and it plays well with complex app structures.

Zakiullah Khan
  • 1,445
  • 2
  • 15
  • 26