简体   繁体   English

无法发送任何 HTML 元素,除了<h1>使用 node.js (Express) 作为对 HTTPS 请求的函数响应

[英]Unable to send any HTML elements other than <h1> as a function response to HTTPS request using node.js (Express)

If I change "< h1 >" to any other HTML tag such as "< h2 > or < p >" it fails to render them.如果我将“< h1 >”更改为任何其他 HTML 标记,例如“< h2 > 或 < p >”,则无法呈现它们。 I have no idea what's wrong.我不知道出了什么问题。

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

const app = express();

// home page
app.get("/", function (req, res) {
  res.sendFile(__dirname + "/index.html");
});

app.post("/", function (req, res) {
  const queryCity = "London";
  // const queryCity = String(req.body.cityName);
  const apiKey = "lorem";
  const url =
    "https://api.openweathermap.org/data/2.5/weather?q=" +
    queryCity +
    "&appid=" +
    apiKey +
    "&units=metric";

  https.get(url, function (response) {
    response.on("data", function (data) {
      const weatherData = JSON.parse(data);

      const temp = weatherData.main.temp;
      const feel = weatherData.main.feels_like;
      const weatherIcon = weatherData.weather[0].icon;
      const iconUrl =
        "https://openweathermap.org/img/wn/" + weatherIcon + "@2x.png";

      res.write(
        "<h1>Temperature in " + queryCity + " is " + temp + "deg Celsius.</h1>"
      );
      res.write("It feels like " + feel + "deg Celsius.");
      res.write("<img src=" + iconUrl + ">");
      res.send(); // there can only be one res.send()
    });
  });
});

// server port
app.listen(3000, function () {
  console.log("Server live on port 3000");
});

this is what happens when I change h1 to h2 or p.这就是当我将 h1 更改为 h2 或 p 时会发生的情况。 Img tag fails too. img 标签也失败了。 What am I doing wrong here?我在这里做错了什么?

This code never fail:这段代码永远不会失败:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.type('text/html');
  res.send('<h1>I am html</h1>');
});

app.listen(process.env.PORT || 8080);

Try to test your html generation with this basic code to be sure that error is not a nodejs issue.尝试使用此基本代码测试您的 html 生成,以确保错误不是 nodejs 问题。

Another tips:另一个提示:

  • replace res.write to res.send like my example像我的例子一样将res.write替换为res.send
  • don't call res.write several times, just one time sending a string previously created with your html lines不要多次调用res.write ,只需一次发送先前用您的 html 行创建的字符串
  • add content type text/html to your response将内容类型 text/html 添加到您的回复中

Had this error as well earlier.之前也有这个错误。 Was able to fix it by adding an html tag on the first res.write .能够通过在第一个res.write上添加一个 html 标签来修复它。

Using your code, it would be like this;使用你的代码,它会是这样的;

res.write(
    "<html><h1>Temperature in " + queryCity + " is " + temp + "deg Celsius.</h1></html>"
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法更改 HTML 元素<h1>到一个<form>作为使用 node.js (Express) 对 HTTPS 请求的函数响应 - Unable to change HTML element <h1> to a <form> as a function response to HTTPS request using node.js (Express) 从express node.js向其他服务器发送请求 - send request to other server from express node.js 在Node.js中使用Express成功后,如何发送JSON响应并重定向到某些html页面? - How to send a JSON response and redirect to some html page after success in Node.js using express? 使用HTTPS和HTTP Express(node.js) - Express (node.js) using HTTPS and HTTP Node.js Express如何以javascript或其他文件类型发送响应 - Node.js Express how do I send response as javascript or other file type 使用express的node.js / Https - node.js / Https with express 如何在Express Node.js中发送对Ajax发布请求的响应?[已回答] - How to send response to ajax post request in express node.js?[answered] express / node.js中是否有响应对象的方法可以将值和数据发送到静态html文件? - Is there a method of the response object in express/node.js that can send values and data to a static html file? 发送响应并继续执行任务 Node.js - Send response and continue to perform tasks Express | Node.js node.js将变量的响应发送到浏览器,而不是控制台 - node.js express send response from variable to the browser and not the console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM