res.write prints html tags as text in express

The solution for “res.write prints html tags as text in express” can be found here. The following code will assist you in solving the problem.

const express = require(“express”);
const https = require(“https”);
const app = express();

const url =
“https://api.openweathermap.org/data/2.5/weather?q=London,uk&units=metric&appid=0333cb6bfed722ca09f1062ec1ea9ca1”;
app.get(“/”, (req, res) => {
https.get(url, response => {
response.on(“data”, data => {
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const { description, icon } = weatherData.weather[0];
const imageURL = `http://openweathermap.org/img/wn/${icon}@2x.png`;

res.set(“Content-Type”, “text/html”);
//OR
res.setHeader(“Content-Type”, “text/html”);

res.send(`

The weather is currently ${description}

The temperature in London is ${temp} ° Celsius.

`);
});
});
//res.send(‘server is up!!!’);
});

app.listen(3000, () => {
console.log(“Server started!!!”);
});

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in