I have a template Hello, ${user.name}
stored in a variable. I am reading this from an external file using fs.read
.
Now, obviously when I attach to the innerHTML of a target div, it shows the string as it is and not "Hello, James" (assuming user.name = James) as intended.
Is there a way to make it happen?
extfile.txt =>
{"A":"`Welcome, ${user.name}`"}
Node.js code =>
fs.readFile(__dirname + '/extfile.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
} else {
let x = JSON.parse(data);
socket.emit('var',x.A);
}
});
HTML =>
socket.on('var',function(x)){
getElementById('target').innerHTML = x;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…