Route configuration error on NodeJS with Express [duplicate]

1

I have a problem when starting NodeJS, similar to this post this link but did not have more feedback from those who asked and the solution presented in the answer did not help me either.

The example is from a book, from Code House, and are simple examples with directory errors but that make me break my mind because I'm starting with Node and Express.

The entire example used the same name as the variables in the book.

Here is the error:

Error: Cannot find module './app/routes/home.js'
at Function.Module._resolveFilename (module.js:326:15)
at Function.Module._load (module.js:277:25)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (E:\projects\contatooh\config\express.js:2:12)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)

contactoh / config / express.js

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

    app.set('port', 3000);
    app.use(express.static('./public'));
    app.set('view engine', 'ejs');
    app.set('views','./app/views');

    home(app);

    return app;
};

contactoh / app / routes / home.js

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

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

UPDATE:

contactoh / app / controllers / home.js

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

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

Directory organization:

contatooh
    app
        controllers
            home.js
        models
        routes
            home.js
        views
            index.ejs
    config
        express.js
    node_modules
        ...
    public

    server.js
    package.json
    
asked by anonymous 11.01.2016 / 20:52

1 answer

1

When you do . you are saying "in this current folder". For example, by doing ./app/controller/home , what you are actually trying to import is a file called home which is located in a folder (or repository) called controller which is a sub-folder of the app folder %, which in turn is in the folder where you are trying to import. But I guess that's not exactly what you wanted to do.

If you find yourself in the file contatooh/app/routes/home.js and want to import app/controller/home (I suppose the app folder is the same in both cases), you have to do a relative import:

var controller = require("../controller/home"); 

.. means the parent (or previous) folder of the folder where you currently find yourself, in which case this folder is app , because you are in routes . After that you only have to specify the path starting from app . If you want to go two folders back, you only have to ../.. , and similarly if you want to go 3 or 4, etc.

I think this is your problem, even though I do not know for sure, because I'm not exactly figuring out the structure of your application.

    
11.01.2016 / 21:48