javascript res.write无法正常工作,它显示的输出包括HTML标记

wxclj1h5  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(92)

我正在使用API和Express制作一个简单的Web应用程序。但我得到的输出与预期的不同。我的输出包含包含HTML标签的文本。

这是我的代码

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=London,uk&units=metric&appid=0333cb6bfed722ca09f1062ec1ea9ca1';
  https.get(url, function(response) {
    console.log(response.statusCode + ' OK');
    response.on('data', function(data) {
      const weatherData = JSON.parse(data);
      const temp = weatherData.main.temp;
      const desc = weatherData.weather[0].description;
      const icon = weatherData.weather[0].icon;
      const imageURL = 'http://openweathermap.org/img/wn/' + icon + '@2x.png';

      res.write('<h3>The weather is currently ' + desc + '</h3>');
      //res.write('<img src=' + imageURL + '>');
      res.write(
        '<h1>The temperature in London is ' +
          '<span>' +
          temp +
          '</span> ° Celsius.</h1>'
      );
      res.send();
    });
  });
  //res.send('server is up!!!');
});

app.listen(3000, function() {
  console.log('Server started!!!');
});

输出:

5rgfhyps

5rgfhyps1#

设置标题:res.set("Content-Type", "text/html");

**建议:**使用模板字符串:(

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(`
      <h3>The weather is currently ${description}</h3>
      <img src="${imageURL}">
      <h1>The temperature in London is <span>${temp}</span> ° Celsius.</h1>
      `);
    });
  });
  //res.send('server is up!!!');
});

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

mrphzbgm2#

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?appid=d44d8ad5bbf91d43d54c12a154921930&q=surat&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
            

            const feels_like=weatherData.main.feels_like
           
            res.setHeader("Content-Type", "text/html");
            res.write("<h3>The Weather is currently "+weatherDescription+"</h3>");
            res.write("<h1>The temperature in Surat is :"+temp+" Degree Celcius</h1>");
            res.send() 

        })
    })
    //res.send("Server is up and running")
})

app.listen(3000,function(){
    
    console.log("Server is rinning on port 3000.");
})
2g32fytz

2g32fytz3#

只需将res.set或res.setHeader与内容类型放在res.write之前,就会给予解决方案。

res.set("Content-Type", "text/html");
res.write('<h3>The weather is currently ' + desc + '</h3>');
// or
res.setHeader("Content-Type", "text/html");
res.write('<h3>The weather is currently ' + desc + '</h3>');

相关问题