Currently diving into web app development, I have ventured into using express and implemented the following code snippet:
app.post("/", function(req, res) {
var crypto = req.body.crypto;
var fiat = req.body.fiat;
var amount = req.body.amount;
var options = {
url: "https://apiv2.bitcoinaverage.com/convert/global",
method: "GET",
qs: {
from: crypto,
to: fiat,
amount: amount
}
};
request(options, function(error, response, body) {
var data = JSON.parse(body);
var time = data.time;
var price = data.price;
res.write("Current time is: " + time);
res.write("<p> Your " + amount + " " + crypto + " is worth " + price +
" " + fiat + "</p>");
res.send();
});
});
However, upon running my server and sending a POST request, the output on the page appears as follows:
Current time is: 2019-06-29 18:50:35 <p>
Your 2 BTC is worth 23954.24 USD</p>
I am aware that res.write() should not render HTML, resulting in visible tags as text.
Have I made an error with my code? How can I rectify this issue so that HTML tags do not display as text on the page?
Your assistance is greatly appreciated :)