Issue when routing a page

I'm having trouble with this script

// App.js ( shrink )

var controller = require('./controller');

app.get('/', controller.index);

app.get('/home', controller.home);

// /controller/index.js

var meta = {

    title: 'index',
    description: ''
}

exports.index = function(req,res){

    res.render('index', {

        meta: meta
    });
}

// /controller/home.js

var meta = {

    title: 'glibet',
    description: ''
}

exports.home = function(req,res){

    res.render('home', {

        meta: meta
    });
}

Its returning me this Error: "Error: Route.get() requires callback functions but got a [object Undefined]"

Strangely enough it works just fine if i let app.get('/', controller.index); alone without the home route

I've tried a couple of corrections/alternatives in the code maintaining the system its way of invoking controller/files but it doesn't seem to fix the code, i will really appreciate any help.

PS: I'm trying to avoid setting a variable to each controller file, avoiding something like this code;

var homeController = require('./controllers/home');
var userController = require('./controllers/user');

app.get('/', homeController.index);
app.get('/login', userController.getLogin);

When you require ./controller, Node first looks for a file called controller. Failing that, it looks for a file called index.js inside the directory controller.

You are not requiring every file in a directory by using require() in this manner.

You could use a plugin such as require-dir for this purpose. However, my recommendation would be to require each file separately, so that you know exactly what you are making available to your page-- this prevents side effects where you accidentally leave a file in your routes directory that you didn't want required by your main application controller.

You are trying to require a directory of js files as an object. If you want to keep all of your controller files separate but have them represented in one object in your app, you could do something like this:

// /controller/controllers.js

var home = require('./home');
var index = require('./index');
module.exports = {
    home: home.home,    
    index: index.index
}

By requiring './controller/controllers' you could keep all of your controller endpoints organized in different files while accessible in one object.