我试图更好地体验nodeJS,而且我不想将所有脚本放在一个文件中。
所以,我使用了这个结构
./
public/
css/
styles.css
images
views
index.ejs
profile.ejs
app.js
routes.js
我的档案现在:
app.js
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
// routes
require('./routes.js')(app);
app.listen(1337);
在routes.js中,如果我使用 res.render(' index'),那么当我使用 res.render('个人资料& #39;)然后它给出错误无法查找视图"个人资料"在视图目录中
routes.js ----- 工作
module.exports = function(app) {
app.get('/', function(req, res) {
res.render('index');
});
};
routes.js ----- 不工作
module.exports = function(app) {
app.get('/', function(req, res) {
res.render('profile');
});
};
假设 index.ejs 和 profile.ejs 具有相同的内容。
如果我在routes.js中使用 res.render('个人资料'),有人可以解释是什么问题
编辑:完成错误消息:
Error: Failed to lookup view "profile" in views directory "/home/anand03/Desktop/mynode/views"
at EventEmitter.render (/home/anand03/Desktop/mynode/node_modules/express/lib/application.js:579:17)
at ServerResponse.render (/home/anand03/Desktop/mynode/node_modules/express/lib/response.js:960:7)
at /home/anand03/Desktop/mynode/routes2.js:10:12
at Layer.handle [as handle_request] (/home/anand03/Desktop/mynode/node_modules/express/lib/router/layer.js:95:5)
at next (/home/anand03/Desktop/mynode/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/anand03/Desktop/mynode/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/anand03/Desktop/mynode/node_modules/express/lib/router/layer.js:95:5)
at /home/anand03/Desktop/mynode/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/anand03/Desktop/mynode/node_modules/express/lib/router/index.js:330:12)
at next (/home/anand03/Desktop/mynode/node_modules/express/lib/router/index.js:271:10)