2017-09-23 89 views
0

我對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> 

我在別處找不到一個確切的答案。

回答

0

嘗試這樣,

app.get('/profile/:name', function(req, res) { 
    res.render('profile'); // see the change here for *res* 
}); 
+0

Thaank你這麼洙多! 它真的爲我工作:) – Pranshu