Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js-在ETIMEDOUT中请求大量数据结果_Javascript_Node.js_Request - Fatal编程技术网

Javascript Node.js-在ETIMEDOUT中请求大量数据结果

Javascript Node.js-在ETIMEDOUT中请求大量数据结果,javascript,node.js,request,Javascript,Node.js,Request,我确实请求api端点,该端点包含大量数据,需要一些时间来获取。我的请求有时成功,但有时会出错。我尝试增加请求的超时时间,但这并不能解决我的问题。是否有方法以块的形式加载数据或增加服务器超时时间?是的,有方法以块的形式发送响应。您需要使用Node.js流 下面是一个例子 http.createServer(function(req, res) { // The filename is simple the local directory and tacks on the requested u

我确实请求api端点,该端点包含大量数据,需要一些时间来获取。我的请求有时成功,但有时会出错。我尝试增加请求的超时时间,但这并不能解决我的问题。是否有方法以块的形式加载数据或增加服务器超时时间?

是的,有方法以块的形式发送响应。您需要使用Node.js流

下面是一个例子

http.createServer(function(req, res) {
  // The filename is simple the local directory and tacks on the requested url
  var filename = __dirname+req.url;

  // This line opens the file as a readable stream
  var readStream = fs.createReadStream(filename);

  // This will wait until we know the readable stream is actually valid before piping
  readStream.on('open', function () {
  // This just pipes the read stream to the response object (which goes to the client)
  **readStream.pipe(res);**
  });

  // This catches any errors that happen while creating the readable stream (usually invalid names)
  readStream.on('error', function(err) {
    res.end(err);
      });
  }).listen(8080);
以上代码来自Node.js文档

关注“readStream.pipe(res);”部分

在读取文件的同时不断发送res。如果文件很大,它仍然能够缓慢而连续地发送到客户端

检查文件


类似地,您还可以允许客户端对大型视频文件进行流式处理,比如说,使用这个流式处理750MB。只是,处理视频还有一些更复杂的问题。

使用模块
http
http.request()
可以设置超时,如下所述:

注意:您可以在回调中的chunk
res.on('data',…)
事件中加载数据,如:

const req = http.request(options, (res) => {
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});
req.end();

代码和更多详细信息位于:

正如人们已经提到的,使用

假设一个客户端从我们的服务器请求一个大文件(下面代码中的
file.txt
)。使用流,我们将文件分块发送,而不是在内存中缓冲。服务器并没有消耗太多的RAM,客户端得到了即时响应,每个人都很高兴

const fs = require("fs");
const http = require("http");
const server = http.createServer();
const port = 8000;

server.on("request", (req, res) => {
  const src = fs.createReadStream("./file.txt"); // file.txt is some big file
  src.pipe(res);
  fs.readFile("./file.txt", (err, data) => {
    if (err) throw err;
    res.end(data);
  });
});

server.listen(port, () => { `Server is listening on http://localhost:${port}` });

你能添加你的代码吗?显示您是如何尝试增加超时的。嘿@shaunaa,您在服务器端是否也有控制权,或者您只是尝试从第三方API获取数据?我只是尝试从第三方API获取数据。这是用于尝试从第三方API获取数据的?不,这是将大量数据从服务器发送到客户端。要从第三方API获取数据,请以相反的方式使用流来读取响应。通过@qphiYes检查答案,使用请求的流应该可以工作。我也想到了流,但我要求在服务端点中使用。但是,由于这是一个第三方API,您的解决方案应该是好的。