无法正常工作。它显示的输出包括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标记
发布评论