Route configuration error in Nodejs with Express

Asked

Viewed 1,094 times

1

I’m having a problem starting Nodejs, similar to this other post at this link, but had no more feedback from those who asked and the solution presented in the reply did not suit me either.

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

The whole example was used the same name of the book variables.

That 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)

contactooh/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;
};

contactooh/app/Routes/home.js

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

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

UPDATE:

contactooh/app/controllers/home.js

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

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

Organization of directories:

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

    server.js
    package.json

1 answer

1


When you do . you are saying "in this current folder". For example, doing ./app/controller/home, what is actually trying to import is a file that is called home which is located in a folder (or repository) called controller which is a subfolder of the folder app, which in turn is in the folder you’re trying to import from. But I don’t think that’s exactly what you wanted to do.

If you are for example in the file contatooh/app/routes/home.js and you want to import app/controller/home (I suppose the briefcase app be the same in both cases), what you have to do is make an "import relative":

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

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

I think that’s your problem, even though I’m not absolutely sure, because I’m not really understanding what the structure of your application is.

  • I will try to change the path of the folders, return later if it works

  • I did a post and code update. I changed the express.js line of: var home = require('./app/routes/home') to var home = require('../app/routes/home'); . And also the 'Routes/home.js' of: var controller = require('./app/controllers/home'); for var controller = require('../controllers/home'); and stopped giving directory error. Thanks.

Browser other questions tagged

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