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
I will try to change the path of the folders, return later if it works
– O_Vagner
I did a post and code update. I changed the express.js line of:
var home = require('./app/routes/home')
tovar home = require('../app/routes/home');
. And also the 'Routes/home.js' of:var controller = require('./app/controllers/home');
forvar controller = require('../controllers/home');
and stopped giving directory error. Thanks.– O_Vagner