Nodejs route configuration using Express 4.8

Asked

Viewed 1,519 times

4

I am using version v0.10.37 of Node and 4.8 of Express. I am trying to set the route to index. And the following errors appear

Error: Route.get() requires callback functions but got a [Object Undefined]

Or:

Error: Cannot find module './app/Routes/home'

app/controller/home.js

module.exports = function() {
    var controller = {};
    index: function(req, res) {

        res.render('index', {nome: 'Express'});
    };

    return controller; 
};

app/Routes/home.js

var controller = require('./app/controllers/home');
module.exports = function(app) {

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

};

config/express.js

var express = require('express');
var home = require('./app/routes/home');
module.exports = function() {
    var app = express();

    app.set('port', 3000);
    app.set('view engine', 'ejs');
    app.set('views', './app/views');
    //middleware

    home(app);

    return app;
};
  • Alane, did you solve this problem? The answer helped?

1 answer

3

You have some errors in your code.

Your first mistake doesn’t seem to be related to the code you show in the question. Route.get() implies using i router from express and I don’t see it in your code.

There are two common ways to manage routes in a modular way in different files. One of them is with Routes, such as in this question, the other is how you’re using it (with a few mistakes I’ll explain below).

app/controller/home.js

In this file you want to create and export the right controler object? and when the module is called it must return an object with property, one of which the index which has to be a function with parameters req, res and optional next.

In this case the syntax of this file should be like this:

var controller = {
    index: function(req, res) {
        res.render('index', {nome: 'Express'});
    }
};
module.exports = controller;

problems finding files

On Node.js when you use one require('./pasta/subpasta/ficheiro.js'); he always leaves the current board. So ./ is for files in the same directory, ../ down a folder. So yours path are wrong, I think you need to require('../etc'); the two that you have so first they have to go down the folder and then go up.

So the complete code should be:

app/controller/home.js // <- with or without "s" ??

var controller = {
    index: function(req, res) {
        res.render('index', {nome: 'Express'});
    }
};
module.exports = controller;

app/Routes/home.js

var controller = require('../controllers/home'); // <- com ou sem "s" ??
module.exports = function(app) {

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

};

config/express.js

var express = require('express');
var home = require('../app/routes/home');
module.exports = function() {
    var app = express();

    app.set('port', 3000);
    app.set('view engine', 'ejs');
    app.set('views', './app/views');
    //middleware

    home(app);

    return app;
};
  • Oops, Sergio, thank you. I saw the module.Xports issue yesterday. But now, regarding the paths, even going back to the directories, the error remains. Only when step the absolute path works. Do you have any suggestions? and the use of __dirname and __filename?

  • @Alanepontes noticed the path problem with the word "controler"? sometimes you have the singular in others in the plural. Still wrong? what error gives?

Browser other questions tagged

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