我的代码和 ejs 渲染有问题
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var path = require("path");
//View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(__dirname + '/views'));
var users = "John Doe";
app.get('/', function(req, res){
var title = 'Customers';
res.render('index', {
title: title,
users: users
});
console.log(Date() + " accessed Site");
});
app.listen(8080);
console.log("Running at port 8080...");
这似乎是这么简单的代码,但它不起作用。这是我的 index.ejs
<!DOCTYPE html>
<html lang="de" dir="ltr">
<head>
<% include partials/includes %>
<meta charset="utf-8">
<title>{%= title %}</title>
</head>
<body>
<% include partials/header %>
<h1>Alles hat ein Anfang nur die Wurst... wait a second!</h1>
<h2>{%= users %}</h2>
</body>
</html>
所以问题是,我的变量 title
和 users
不想被渲染。
我不知道我错过了什么。
最佳答案
使用
<%= title %>
或 <%- title %>
回显页面上的值。
一般来说,我们使用<%= %>
或 <%- %>
在页面上显示发送的变量/对象
关于javascript - node.js 和 ejs 渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956102/