更新:如果我通过指定地址/ edit.html而不是地址/编辑来访问edit.html,则它可以正常工作。
我在提供index.html以外的html文件时遇到问题。
我的server.js看起来像这样:
// EXPRESS CONFIG
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.static(__dirname + "/public/views"));
app.use(express.static(__dirname + "/public/js"));
app.use(express.static(__dirname + "/public/css"));
app.use(express.static(__dirname + "/public/media"));
app.engine("html", require("ejs").renderFile);
// LOAD ROUTES
require("./server/routes/index")(app);
require("./server/routes/edit")(app);
require("./server/routes/dev")(app);
require("./server/routes/stat")(app);
require("./server/routes/contacts")(app);
require("./server/routes/remarks")(app);
路线示例(edit.js)
'use strict';
module.exports = function(app) {
app.get("/edit", function (req, res) {
res.render("edit.html");
});
};
尝试通过导航到网站/编辑路线来访问HTML文件会产生500内部服务器错误
Error: Failed to lookup view "edit.html" in views directory "C:\Users\andersl\Desktop\informr-test/views" at Function.app.render (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\application.js:493:17) at ServerResponse.res.render (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\response.js:798:7) at C:\Users\andersl\Desktop\informr-test\server\routes\edit.js:6:13 at callbacks (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\router\index.js:164:37) at param (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\router\index.js:138:11) at pass (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\router\index.js:173:5) at Object.router (C:\Users\andersl\Desktop\informr-test\node_modules\express\lib\router\index.js:33:10) at next (C:\Users\andersl\Desktop\informr-test\node_modules\express\node_modules\connect\lib\proto.js:193:15) at SendStream.error (C:\Users\andersl\Desktop\informr-test\node_modules\express\node_modules\connect\node_modules\serve-static\index.js:74:37)
以下app.use组合也不成功:
app.use(express.static(__dirname + '/public'));
app.use("/public", express.staticProvider(__dirname + '/public'));
奇怪的是index.html工作正常,但只有那条路线。
我的应用程序结构如下:
server.js
/public
/views - all html files
/js
/css
/media
/server
/routes
etc...
什么是我不明白,解决方案是什么?
感谢。
答案 0 :(得分:2)
设置视图路径是解决方案。
app.set('views', __dirname + '/public/views');