jeudi 23 juillet 2020

How do I format lines written in response.write() in NodeJS?

I am a beginner to NodeJS and express and was following a tutorial where I had to print two lines on the browser which are both headings. Since res.send() cannot be written twice, my instructor introduced us to write method. When she uses it exactly as I have used it, she gets a proper heading with the required formatting. Meanwhile, I get this :

enter image description here

const express = require("express");
const https = require("https");

const app = express();

app.get("/", function (req, res) {

    const url = "https://api.openweathermap.org/data/2.5/weather?q=kathmandu&appid=35ba591e9032a4e3b4a4ed1936293774&units=metric"
    https.get(url, function (response) {
        console.log(response.statusCode)

        response.on("data", function (data) {
            const weatherdata = JSON.parse(data)
            const temp = weatherdata.main.temp
            const weatherDescription = weatherdata.weather[0].description
            res.write("<h3>" + weatherDescription + " </h3>");
            // res.write("<h2>The temperature in Kathmandu is " + temp + " degree celcius</h2> <br/><h3>" + weatherDescription + "</h3>");
            res.send()
        })
    });
})

app.listen(3000, function () {
    console.log("Server is running in port 3000")
})



Aucun commentaire:

Enregistrer un commentaire