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)