I am new in nodeJs When I try to return variable data to the server it's returning to a variable as a string.
Here is my app.js code
var express = require('express');
var app = express();
var dataFile = require('./data/data');
app.set('port', process.env.PORT || 3000);
app.get('/', (req, res) =>{
console.log(dataFile);
var info= '';
dataFile.friends.forEach(function(item){
info +='<li><h2>${item.name}</h2><p>${item.summary}</p></li>';
});
console.log(info)
res.send("<h1>MMS</h1>${info}");
});
var Server = app.listen(app.get('port'), function(){
console.log('listen to port '+app.get('port'));
});
Here is my data.json code
{
"friends": [{
"title" : "Class Friend",
"name": "Ajay Negi",
"shortname" : "Ajay",
"summary" : "Intro",
"description": "<p>Ajay was born in D.Dun </p>"
},{
"title" : "Best Friend ",
"name": "Manpreet",
"shortname" : "Manpreet",
"summary": "Intro",
"description": "<p>Manpreet was born in D.dun</p>"
},{
"title" : "ME",
"name": "Vijay",
"shortname" : "V.J",
"summary": "INTRO",
"description": "<p>Vijay was born in D.dun</p>"
}]
}
When I run my browser its showing
In place of data If I am using js method to make li without $ and retun only info var than its working as I expected...
MMS
${info}"` is a plain string, it's not a string literal that does interpolation if given `${info}`. You need to surround this in backticks `\`` instead of quotes. – VLAZ Jan 13 '20 at 14:14