[英]i can't figure out why we are using response.on
I've written a code here and I want to know what "response.on" is doing and why we are passing "data", what is this "data".我在这里写了一个代码,我想知道“response.on”在做什么以及我们为什么要传递“数据”,这个“数据”是什么。 what exactly that ".on" is used for.
“.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)
it's a event emitter. .on("nameOfEvent", callback)
它是一个事件发射器。 So, basically whenever that event is called, that callback
will be executed.因此,基本上只要调用该事件,就会执行该
callback
。 data
is just the name of the event. data
只是事件的名称。
You using respons
in the argument of function and response
inside of function.您可以使用
respons
在功能和参数response
的函数内。
https
https
https://nodejs.org/api/https.html#https_https_get_options_callback https://nodejs.org/api/https.html#https_https_get_options_callback
You can read that response
is not a classical response from the server but an event listener that listens for stream of data.您可以读到
response
不是来自服务器的经典响应,而是侦听数据流的事件侦听器。
It is a low-level interface and in your applications, you should not use them.它是一个低级接口,不应在您的应用程序中使用它们。 Lets interest in
node-fetch
or axios
instead if you want only get a response, but not process streams in real-time.如果您只想获得响应,而不是实时处理流,则可以对
node-fetch
或axios
感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.