我是MEAN Stack编码的新手,我正在阅读Amos Q.Haviv的一本名为Mean Web Development的参考书,我在下面的代码中遇到了麻烦;
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;
};
此处以下代码段发出错误:
require('../app/routes/index.server.routes.js')(app);
错误如下:
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>
controller.js的代码如下
exports.render = function(req, res) {
console.log('Inside COntroller');
res.send('Hello World');
console.log('Response written);
};
routes.js的代码如下:
module.exports = function(app) {
console.log('inside router.js');
var index = require('../controllers/index.server.controller');
console.log('controller.js');
app.get('/', index.render);
};
express.js的代码如下:
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;
};
server.js的代码如下
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/');
package.json如下
{
"name" : "MEAN",
"version" : "0.0.3",
"dependencies" : {
"express" : "~4.8.8"
}
}
我想知道出现这个错误的原因,我试图搞清楚自己,我找不到原因,因为我不熟悉平均堆栈开发和表达服务器编码。请帮助我解决这个问题,并且在之前提出的问题中与此问题无法匹配。在编码中我使用了水平结构。 文件夹结构如下....
---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>
答案 0 :(得分:0)
按照您提到的书中的说明,我遇到了完全相同的错误。对我来说,问题出在控制器文件名中的空格字符中:
index.server。
controller.js
当我从书中复制文件名以创建该文件时,会出现此空间。文件名在书中的两行上,这导致了这个空间。 如果这与您相关,只需将文件重命名为:
index.server.controller.js