最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 12浏览 0评论

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标记

我正在使用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();
发布评论

评论列表 (0)

  1. 暂无评论