在节点/expression中找不到视图

正如我的用户名所示,我是node.js的新手。 我正在努力学习它。 作为这个过程的一部分,我正在设置一个基本的网站。 这个网站将显示几个基本的网页,并公开一个REST端点。 我的项目的结构是:

config.js home.html start.js routes.js server.js resources css style.css images up.png down.png javascript home.html.js 

start.js有我的主要服务器代码。 该文件通过命令行使用“node start.js”执行。 一旦启动,我的服务器就开始监听端口3000. start.js中的代码如下所示:

 var express = require('express'); var app = express(); var UserProfileHandler = require('./app/handlers/UserProfileHandler'); app.configure(function () { app.engine('html', require('ejs').renderFile); app.set('views', __dirname + '/'); app.use(express.logger({ stream: expressLogFile })); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); var routes = { userProfiles: new UserProfileHandler() }; function start() { routeConfig.setup(app, routes); var port = process.env.PORT || 3000; app.listen(port); console.log("SUCCESS: Server listening on port %d in %s mode", port, app.settings.env); } exports.start = start; exports.app = app; 

我的routes.js文件有以下内容:

 function setup(app, routes) { viewSetup(app); apiSetup(app, routes); } function viewSetup(app) { app.get('/', function (req, res) { res.render("/home.html"); }); app.get('/home.html', function (req, res) { res.render("/home.html"); }); } function apiSetup(app, routes) { app.get('/api/userProfiles/:username', routes.userProfiles.getUserProfiles); } 

我正试图在浏览器窗口中加载home.html。 我试图访问http://localhost:3000http://localhost:3000/http://localhost:3000/home.html 。 不幸的是,这些工作都没有。 事实上,我收到一个错误,说:

Express 500错误:查找视图失败“/home.html”

我知道我很亲密 如果我访问http://localhost:3000/api/userProfiles/me我会收到一个JSON响应,就像我期待的那样。 出于某种原因,我似乎无法返回HTML。 我的home.html文件如下所示。

 <html> <head> <script type='text/javascript' src='/resources/javascript/home.html.js'></script> </head> <body> We're up and running! <img src='/resources/images/up.png' /> </body> </html> 

它是一个非常基本的HTML文件。 即使HTML回来,但我担心JavaScript文件和它引用的图片将无法访问。 我很关心这一点,因为我不太确定Node中的path和工作方式。

我如何获得home.html在我的节点设置工作?

谢谢!

因为你的视图文件与你的主文件位于同一个文件夹中,所以下面的改变应该使它工作

1.更改视图文件夹configuration行

 app.set('views', __dirname + '/');//wont work 

 app.set('views', __dirname);//will work 

2.改变视图渲染线

 res.render("/home.html");//wont work 

 res.render("home.html");//will work 

与这两个变化,视图应该工作正常

更新到下面的评论。

你提到的有关图像,CSS和JS的问题是由于应该改变的静态文件夹configuration

 app.use(express.static(__dirname + '/public')); 

 app.use(express.static(__dirname + '/resources')); 

因为你的静态文件夹被命名为resources

但请确保在您的视图中,您正在引用css / js / image文件

例如:

 /css/style.css /images/up.png /images/down.png /javascript/home.html.js 

从您的视图文件

同样,如果上述的工作,检查你是否已经正确的path,也可以尝试通过采取

 app.use(express.static(__dirname + '/resources')); 

之前

 app.use(express.methodOverride()); app.use(app.router); 

线条喜欢

 app.configure(function () { app.engine('html', require('ejs').renderFile); app.set('views', __dirname + '/'); app.use(express.static(__dirname + '/public')); //try changing the position of above line in app.configure and resatrt node app app.use(express.logger({ stream: expressLogFile })); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); });