我正在尝试在向根目录(/
)发出GET请求时呈现静态HTML页面。
但是,访问根目录(http://localhost:3000/
)时收到以下错误:
TypeError: Cannot read property '_locals' of undefined
项目结构
index.js
node_modules
public
|-- html
| |--index.html
index.js
var express = require('express');
var app = express();
var port = 3000;
app.use(express.static(__dirname + '/public'));
app.listen(port);
console.log('Server running on port ' + port + '.');
app.get('/', function(req, res) {
app.render(__dirname + '/html/index.html');
})
我可以转到http://localhost:3000/html/index.html
来访问我的静态HTML页面。但是,对app.render()
的调用失败。我怀疑这与我传递给app.render()
的目录有关。
答案 0 :(得分:2)
app.render
,它应该是res.render
res.render
应该使用模板引擎呈现内容,例如,小胡子,ejs,jade等。你不应该使用它来呈现纯html文件。res.sendFile
app.get('/', function(req, res) {
res.sendFile(__dirname + '/html/index.html');
});
答案 1 :(得分:0)
只需删除此
即可app.get('/', function(req, res) {
app.render(__dirname + '/html/index.html');
});
再次尝试使用它。
答案 2 :(得分:0)
app.get('/', function(req, res) {
res.render(__dirname + '/html/index.html');
})
res 而不是功能中的应用