1

I'm trying to include all files in a directory called "controllers" which has files with express routes and some other things in them.

The problem is that the routes defined inside those files are not working, but if I paste all the code in the index file(file requiring the controllers) they work just fine.

Here's my code/files:

index.js

// Expressjs
const app = require('express')();

// Load and initialize the controllers.
require('./lib/controllersLoader');

/*
 * Initializing the listener according to the settings in the config.
 */
app.listen(3000, err => {
    // Throwing an exception since the whole app depends on this.
    if (err) throw err;
    console.log(`SERVER: Running on port ${config.server.port}`);
});

lib/controllersLoader.js

const fs = require('fs');

// Getting an Array of the files in the 'controllers' folder.
let files = fs.readdirSync( __dirname + '/../controllers');

files.forEach( fileName => {
    require( __dirname + '/../controllers/' + fileName );
});

controllers/index.js

const app = require('express')();
const debug = require('../config').debug;

app.get('/', (req, res) => {
    res.send('Hello, World!');
});
Samuel E.
  • 2,320
  • 2
  • 26
  • 31

2 Answers2

1

Inside your controller file, you are creating an express "subapp", attaching a route to it, and then doing nothing with it.

You should:

  1. Pass your app as parameter in the require, and attach routes to your original express app
  2. Return the subapp, or a router, and mount/use it from your main express app



Example:

index.js

// Expressjs
const app = require('express')();

// Load and initialize the controllers.
require('./lib/controllersLoader')(app);

lib/controllersLoader.js

const fs = require('fs');

// Getting an Array of the files in the 'controllers' folder.
let files = fs.readdirSync( __dirname + '/../controllers');

module.exports = app => {
    files.forEach( fileName => {
        require( __dirname + '/../controllers/' + fileName )(app);
    });
}

controllers/index.js

const debug = require('../config').debug;

module.exports = app => {
    app.get('/', (req, res) => {
        res.send('Hello, World!');
    });
}
Aramil Rey
  • 3,387
  • 1
  • 19
  • 30
  • Thanks, that's what I did, however, ill try to find a "cleaner" solution, if there is any... – Samuel E. Sep 28 '16 at 14:29
  • Maybe [express routers](http://expressjs.com/es/api.html#router) might help you make your controller files cleaner, the other files depends on your structure, you can change it as you wish! :) – Aramil Rey Sep 28 '16 at 14:41
  • Thanks, I used the routers, it's much cleaner – Samuel E. Sep 28 '16 at 18:00
0

I think you're initializing new express instances each time you're requiring it like this:

const app = require('express')();

Can you use only one express instance and pass it to your controllers ? Something like so:

module.exports = function(app){

    app.get('/', (req, res) => {
        res.send('Hello, World!');
    });
}

and to call them:

require( __dirname + '/../controllers/' + fileName )(app);

Your problem seems quite similar to this one : https://stackoverflow.com/a/6059938/734525

Community
  • 1
  • 1
nioKi
  • 1,259
  • 9
  • 17