这是我的server.js
:
var browserify = require('browserify-middleware');
var express = require('express');
var app = express();
var path = require("path");
app.listen(8080);
console.log('Listening at http://localhost:8080');
// routes will go here
app.get('/render', function(req, res) {
res.sendFile(path.join(__dirname+'/public/index.html'), {text: "sfsdfsf"});
});
这是我尝试获取变量的方法text
in my index.html
,但没有一个有效:
<h1>{{ text }} </h1>
<h1><% text %> </h1>
<script type="text/javascript">
var text = "<%= text %>";
var text2 = "{{ text }}";
</script>
有什么想法我哪里出错了吗?
选项1:我建议添加模板引擎,例如pug。模板引擎将模板文件中的变量替换为实际值。要实施哈巴狗模板,请访问https://expressjs.com/en/guide/using-template-engines.html https://expressjs.com/en/guide/using-template-engines.html
选项2:
'use strict';
var express = require('express');
var path = require("path");
var app = express();
const template = (text) => {
return `
<!DOCTYPE html>
<html>
<head>
<title>sample</title>
</head>
<body>
<h1>${text}</h1>
</body>
</html>`;
}
app.get('/render', function(req, res) {
res.send(template('Hello'));
});
app.listen(8080);
console.log('Listening at http://localhost:8080');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)