Doubt with node.js - Error: route.js: 162

1

I'm having trouble running my application. I made the right mappings, according to the book in which I am learning (MEAN stack of the Code House).

Express.js file

// config/express.js
var express = require('express');
var home = require('../app/routes/home');

module.exports = function () {

    //Utilizando o Express
    var app = express();

    //Variaveis de Ambiente
    app.set('port', 3000);

    //Middlewares
    app.use(express.static('./public'));

    //Template Engines
    app.set('view engine', 'ejs');
    app.set('views', './app/views');

    //Rotas
    home(app);

    //Retornando a aplicação
    return app;
};

Route file (home.js)

// app/routes/home.js
var controller = require('../controllers/home');

module.exports = function (app) {
    app.get('/index', controller.index);
    app.get('/', controller.index);   
}

Controller file (home.js)

// app/controllers/home.js

module.exports = function () {
    var controller = {};

    controller.index = function (req, res) {
        // Retorna a página index.ejs
        res.render('index', {nome: 'Express'});
    };

    return controller;
}

I just put the server.js file here

// server.js
var http = require('http');
var app = require('./config/express')();

http.createServer(app).listen(app.get('port'), function() {
    console.log('Express Server escutando na porta ' + app.get('port'));    
});

And lastly, error presented:

C:\Sandbox\contatooh>node server.js
C:\Sandbox\contatooh\node_modules\express\lib\router\route.js:162
        throw new Error(msg);
        ^

Error: Route.get() requires callback functions but got a [object Undefined]
    at C:\Sandbox\contatooh\node_modules\express\lib\router\route.js:162:15
    at Array.forEach (native)
    at Route.(anonymous function) [as get] (C:\Sandbox\contatooh\node_modules\express\lib\router\route.js:158:15)
    at Function.app.(anonymous function) [as get] (C:\Sandbox\contatooh\node_modules\express\lib\application.js:421:19)
    at module.exports (C:\Sandbox\contatooh\app\routes\home.js:5:9)
    at module.exports (C:\Sandbox\contatooh\config\express.js:21:5)
    at Object.<anonymous> (C:\Sandbox\contatooh\server.js:3:38)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    
asked by anonymous 07.07.2016 / 22:40

2 answers

0

You are setting the controler badly. Notice that you have a function:

var x = function () {
    var controller = {};
    controller.index = 'bar';
    return controller;
}

and you are trying to directly access controller.index from outside the function. You have two solutions, or you use x().index and in this case you invoke the function so that it can return and return you the object controler ; or else you export the controler object directly. I would use the second option and in that case your code would look like this:

// app/controllers/home.js
var controller = {};
controller.index = function (req, res) {
    // Retorna a página index.ejs
    res.render('index', {nome: 'Express'});
};
module.exports = controller;
    
08.07.2016 / 00:17
0

Try to pass the controller into exports:

var controller = require('../controllers/home');
    
08.07.2016 / 00:00