我不明白为什么我们要使用 response.on

i can't figure out why we are using response.on

我在这里写了一段代码,我想知道“response.on”是干什么的,为什么要传递“数据”,这个“数据”是什么。 “.on”到底是用来做什么的。

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&appid=5c8618a9210a11846accc217f3b3084f";
  https.get(url,function(respons){
    response.on("data",function(data){
      const wht = JSON.parse(data)
      temp = wht.main.temp
      res.send("temp   "+ temp)
    })
  })
})
app.listen(3000, function(){
  console.log("running");
});

.on("nameOfEvent", callback) 它是一个事件发射器。因此,基本上只要调用该事件,就会执行 callbackdata 只是事件的名称。

  1. 你的代码有错误。

您在函数的参数中使用了 respons,在函数内部使用了 response

  1. 等待响应数据记录在https

https://nodejs.org/api/https.html#https_https_get_options_callback

您可以看到 response 不是来自服务器的经典响应,而是监听数据流的事件监听器。

它是一个 low-level 接口,在您的应用程序中,您不应使用它们。如果您只想获得响应而不是处理 real-time 中的流,请改用 node-fetchaxios