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

编程入门 行业动态 更新时间:2024-10-28 04:15:52

res.write<a href=https://www.elefans.com/category/jswz/34/1771305.html style=无法正常工作。它显示的输出包括HTML标记"/>

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

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

这是我的代码。

const express = require('express');
const https = require('https');
const app = express();

app.get('/', function(req, res) {
  const url =
    '.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 = '/' + 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!!!');
});

输出:

回答如下:

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

res.set("Content-Type", "text/html");
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.write无法正常工作。它显示的输出包括HTML标记

本文发布于:2024-05-07 03:15:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   标记   工作   res   write

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!