2017-03-27 90 views
1

我基本上是學習的Node.js和EJS和我有這樣的代碼:EJS模板呈現出什麼

index.js:

var express = require('express'); 


var app = express(); // createApplications(); 

app.set('view engine', 'ejs') 

app.get('/', function(req,res){ 
    res.sendFile(__dirname + '/index.html'); 
}) 

app.get('/profile/:name', function(req,res){ 
    console.log(req.params.name); 
    res.render("profile", {username : req.params.name}); 
}) 

app.listen(process.env.PORT, process.env.IP, function(err){ 
    if(err) 
     throw err; 
    console.log("Listening at IP:PORT " + process.env.IP + ':' + process.env.PORT) 
}); 

的意見/ profile.ejs:

<!DOCTYPE html> 
<html> 
    <head> 
     <style> 
      body { 
       background : skyblue; 
      } 
     </style> 
    </head> 

    <body> 
     <h5>Profile of <?= username ?> </h5> 
    </body> 
</html> 

當我去個人資料/某人..它說..的配置文件和空白。林混淆了爲什麼它不渲染通過對象res.render("profile", {username : req.params.name});傳遞的用戶名。那console.log測試到req.params.name是好的。

+2

我認爲它作爲你的模板是錯誤的一樣簡單,我認爲brakets應該是'<%=用戶名%>' 我不能在此刻測試,以便只評論 –

+0

@olly_uk ..哇如何我做到了嗎? ...:/。好,謝謝 – amanuel2

回答

1

您正在使用'?'作爲分隔符而不進行配置。

ejs.delimiter = '?'; // add this line 
app.set('view engine', 'ejs'); 
1

變化

<body> <h5>Profile of <?= username ?> </h5> </body> </html>

<body> <h5>Profile of <%= username %> </h5> </body> </html>

它應該正確顯示您的變量。 您使用了錯誤的語法!