res write prints html tags as text in express

Solutions on MaxInterview for res write prints html tags as text in express by the best coders in the world

showing results for - "res write prints html tags as text in express"
Beck
04 Nov 2019
1const express = require("express");
2const https = require("https");
3const app = express();
4
5const url =
6  "https://api.openweathermap.org/data/2.5/weather?q=London,uk&units=metric&appid=0333cb6bfed722ca09f1062ec1ea9ca1";
7app.get("/", (req, res) => {
8  https.get(url, response => {
9    response.on("data", data => {
10      const weatherData = JSON.parse(data);
11      const temp = weatherData.main.temp;
12      const { description, icon } = weatherData.weather[0];
13      const imageURL = `http://openweathermap.org/img/wn/${icon}@2x.png`;
14
15      res.set("Content-Type", "text/html");
16      //OR
17      res.setHeader("Content-Type", "text/html");
18
19      res.send(`
20      <h3>The weather is currently ${description}</h3>
21      <img src="${imageURL}">
22      <h1>The temperature in London is <span>${temp}</span> ° Celsius.</h1>
23      `);
24    });
25  });
26  //res.send('server is up!!!');
27});
28
29app.listen(3000, () => {
30  console.log("Server started!!!");
31});
32