【问题标题】:i can't figure out why we are using response.on我不知道我们为什么要使用 response.on
【发布时间】:2023-03-30 14:50:02
【问题描述】:

我在这里写了一个代码,我想知道“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");
});

【问题讨论】:

    标签: javascript node.js express node-modules


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      1. 在您的代码中存在错误。

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

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

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

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

      它是一个低级接口,在您的应用程序中,您不应该使用它们。如果您只想获得响应而不是实时处理流,请关注 node-fetchaxios

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 2014-06-22
        • 1970-01-01
        相关资源
        最近更新 更多