I am new to coding in MEAN Stack and I am following a reference book called Mean Web Development by Amos Q. Haviv and I have trouble in the following code;
var express = require('express'); module.exports = function() { console.log('inside express.js'); var app = express(); require('../app/routes/index.server.routes.js')(app); console.log('requirement routes.js gathered'); console.log('call app'); return app; };
Here the following code segment is giving an error :
require('../app/routes/index.server.routes.js')(app);
The error is as follows:
D:\\OpenSource\\mean\\MEAN App\\DemoApps\\App-05>node server read .config/express inside express.js inside router.js module.js:338 throw err; ^ Error: Cannot find module '../controllers/index.server.controller' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at module.exports (D:\\OpenSource\\mean\\MEAN App\\DemoApps\\App-05\\app\\routes\\in dex.server.routes.js:3:13) at module.exports (D:\\OpenSource\\mean\\MEAN App\\DemoApps\\App-05\\config\\expres s.js:5:48) at Object.<anonymous> (D:\\OpenSource\\mean\\MEAN App\\DemoApps\\App-05\\server.js :3:11) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) D:\\OpenSource\\mean\\MEAN App\\DemoApps\\App-05>
Code for the controller.js is as follows
exports.render = function(req, res) { console.log('Inside COntroller'); res.send('Hello World'); console.log('Response written); };
Code for the routes.js is as follows:
module.exports = function(app) { console.log('inside router.js'); var index = require('../controllers/index.server.controller'); console.log('controller.js'); app.get('/', index.render); };
Code for the express.js is follows
var express = require('express'); module.exports = function() { console.log('inside express.js'); var app = express(); require('../app/routes/index.server.routes.js')(app); console.log('requirement routes.js gathered'); console.log('call app'); return app; };
And the code for the server.js is as follows
var express = require('./config/express'); console.log('read .config/express'); var app = express(); console.log('express()'); app.listen(3000); console.log('listen 3000'); module.exports = app; console.log('Server running at http://localhost:3000/');
And the package.json is as follows
{ "name" : "MEAN", "version" : "0.0.3", "dependencies" : { "express" : "~4.8.8" } }
I want to know the reason for getting that error and I tried figuring out myself and I couldn't find the reason as I am new to the mean stack development and express server coding. Please help me to solve this problem and there was no match to this question in the previously asked questions. In coding I used the horizontal structure. Folder structure is as follows....
---app</br> ------models</br> ------views</br> ------controllers</br> ------routes</br> ---config</br> -----env</br> -----express.js</br> ---node_modules(generated by npm)</br> ---public</br> ---package.json</br> ---server.js</br>
I had exactly the same error when following instructions from the book you mentioned. For me the problem was in a space character in the file name for the controller:
index.server.
controller.js
This space occurred when I copypasted the file name from the book to create that file. The file name was on two lines in the book, and that caused that space. If this is relevant for you, just rename the file to be:
index.server.controller.js
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.