如何在不使用任何模板引擎的情况下在另一个 HTML 页面上显示一个 HTML 页面的 POST 数据值?以下是发布数据的代码:
app.post('/RegistrationSuccessPage', function (req, res) {
var uname = req.body.username
var pwd = req.body.pwd
var emailAddress = req.body.email
postData = uname+","+pwd+","+emailAddress
console.log(postData);
res.sendFile( __dirname + "/RegistrationSuccessPage.html",postData);
});
下面是 HTML,我必须在其中显示值:
<body>
<div>
<form>
<div align="center">
<label>Below are the details</label><br><br>
<label id="uname">Username *: (username_value_to_be_shown_here)</label><br><br>
<label id="email">Email Address *: (email_value_to_be_shown_here)</label><br><br>
<br><br>
</div>
</form>
</div>
</body>
最佳答案
如果您通过 $USER
更改 username_value_to_be_shown_here
并通过 $EMAIL
更改 email_value_to_be_shown_here
您可以使用如下字符串替换:
var fs = require('fs');
app.post('/RegistrationSuccessPage', function (req, res) {
var data = {
USER: req.body.username,
EMAIL: req.body.email
};
fs.readFile( __dirname + '/RegistrationSuccessPage.html', 'utf8', function(err, content) {
var result = content;
for (var key in data) {
result = result.replace("$" + key, data[key]);
}
res.send(result);
});
});
如果 $VAR 位于多个地方,您将需要使用正则表达式,例如:
result = result.replace(new RegExp("\\$" + key, "g"), data[key]);
关于javascript - 如何在不使用任何模板引擎的情况下在 HTML 页面上显示帖子数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546419/