EJS模板什么都不呈现
我基本上学习Node.js和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) });
views / profile.ejs:
<!DOCTYPE html> <html> <head> <style> body { background : skyblue; } </style> </head> <body> <h5>Profile of <?= username ?> </h5> </body> </html>
当我去configuration文件/某人..它说configuration文件..和空白。 林混淆了为什么它没有渲染通过对象传递的用户名res.render("profile", {username : req.params.name});
。 那console.logtesting到req.params.name
是好的。
您正在使用 '?' 作为分隔符而不进行configuration。
ejs.delimiter = '?'; // add this line app.set('view engine', 'ejs');
更改
<body> <h5>Profile of <?= username ?> </h5> </body> </html>
至
<body> <h5>Profile of <%= username %> </h5> </body> </html>
它应该正确显示你的variables。 你使用了错误的语法!