1
I am facing problems running my application. I performed the correct mapping, according to the book in which I am learning (MEAN stack 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;
};
File route (home.js)
// app/routes/home.js
var controller = require('../controllers/home');
module.exports = function (app) {
app.get('/index', controller.index);
app.get('/', controller.index);
}
File controller (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;
}
Just in case I put here also the file server.js
// 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 finally, 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)
I tried however unsuccessfully, unfortunately.
– Lucas Monteiro Inácio
This error occurs when you are trying to use an object that has not yet been defined. Check whether the path of your requires actually arrives in the modules
– guijob