Doubts with Node.js - Error: route.js:162

Asked

Viewed 231 times

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)

2 answers

0

Try passing the controller into the Exports:

var controller = require('../controllers/home');
  • I tried however unsuccessfully, unfortunately.

  • 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

0


You are malfunctioning the control module. Note that you have a function:

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

and you’re trying to access it directly controller.index out of function. You have two solutions, or you use x().index and in this case you invoke the function so that it can do return and return the object to you controler; or export the object controler 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;
  • 1

    Got it this way, thanks man!

  • @Lucasmonteiroinácio great! I’m glad I can help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.