【发布时间】:2017-09-23 17:50:45
【问题描述】:
我是 Node.js 的新手,现在正在学习 Express 框架。 但我再次陷入了一个非常恼人的错误。 我正在从一个视频教程中学习,在该视频教程中我正在学习如何使用模板引擎“EJS”,并且正在和平地制作一个非常简单的程序,直到遇到这个错误。
TypeError: Cannot read property '_locals' of undefined
at EventEmitter.render
(G:\Docs\Node.js\node_modules\express\lib\application.js:549:11)
at G:\Docs\Node.js\app.js:6:9
at Layer.handle [as handle_request]
(G:\Docs\Node.js\node_modules\express\lib\router\layer.js:95:5)
at next (G:\Docs\Node.js\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch
(G:\Docs\Node.js\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(G:\Docs\Node.js\node_modules\express\lib\router\layer.js:95:5)
at G:\Docs\Node.js\node_modules\express\lib\router\index.js:281:22
at Function.process_params
(G:\Docs\Node.js\node_modules\express\lib\router\index.js:335:12)
at next (G:\Docs\Node.js\node_modules\express\lib\router\index.js:275:10)
at expressInit
(G:\Docs\Node.js\node_modules\express\lib\middleware\init.js:40:5)
我的代码是这样的:-
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
app.set('views', __dirname, '/templates');
app.get('/', function(req, res) {
app.send("Welcome to the Homepage");
});
app.get('/profile/:name', function(req, res) {
app.render('profile');
});
app.listen(4242);
console.log("Server is running ....");
我在当前目录中的模板文件夹中创建了一个 profile.ejs 文件,当我输入 127.0.0.1/4242/profile/anyname 时我想要它 必须呈现并显示该个人资料页面。
我的 Profile.ejs 页面是:
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
</head>
<body>
<h1>Welcome to the Profile !!</h1>
</body>
</html>
我在别处找不到确切的答案。
【问题讨论】:
标签: javascript node.js web-development-server