javascript - NodeJs 在外部数据请求时内存不足

标签 javascript node.js axios

我正在使用 Axios 在后端执行大型数据提取:

await Axios({
      method: 'GET',
      url,
      headers: {
        'content-type': 'application/json',
        Authorization: `Bearer ${forgeAccessToken}`,
      },
    });

NodeJS 崩溃。内存问题:

  1|rest-backenq  | FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

我将内存从 4GB 增加到 16GB。

没有效果。

我无法控制从中获取数据的外部 API。

我该怎么做才能解决这个问题?

最佳答案

我认为你的文件正在下载到你的内存中,使用 request 模块而不是 Axios 来下载文件

var fs = require('fs');
var request = require('request');
var progress = require('request-progress');

// The options argument is optional so you can omit it 
progress(request({
    url : url,
    headers: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${forgeAccessToken}`
    }
}), {
    // throttle: 2000,                    // Throttle the progress event to 2000ms, defaults to 1000ms 
    // delay: 1000,                       // Only start to emit after 1000ms delay, defaults to 0ms 
    // lengthHeader: 'x-transfer-length'  // Length header to use, defaults to content-length 
})
.on('progress', function (state) {
    // The state is an object that looks like this: 
    // { 
    //     percent: 0.5,               // Overall percent (between 0 to 1) 
    //     speed: 554732,              // The download speed in bytes/sec 
    //     size: { 
    //         total: 90044871,        // The total payload size in bytes 
    //         transferred: 27610959   // The transferred payload size in bytes 
    //     }, 
    //     time: { 
    //         elapsed: 36.235,        // The total elapsed seconds since the start (3 decimals) 
    //         remaining: 81.403       // The remaining seconds to finish (3 decimals) 
    //     } 
    // } 
    console.log('progress', state);
})
.on('error', function (err) {
    // Do something with err 
})
.on('end', function () {
    // Do something after request finishes 
})
.pipe(fs.createWriteStream('IE11.Win8.1.For.Windows.VirtualBox.zip'));

有关更多信息,请关注此问题, What is way to download big file in NodeJS?

关于javascript - NodeJs 在外部数据请求时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145591/

相关文章:

javascript - Bootstrap 折叠点击后不折叠

javascript - 具有 Ng-Repeat 的动态 Ng 模型

javascript - 如何将 JavaScript 与 Razor 代码混合使用

javascript - 使用 React 和 Express 阻止跨域请求

node.js - NodeJS : How to stop a Sails MVC app?

javascript - 使用 AWS Javascript 开发工具包发送 SMS

typescript - Axios 拦截器 - 如何从 vuex 存储返回响应

node.js - 如何忽略 Node 请求中的SSL证书验证?

node.js - 每当我在更改映射组件后重新获取 react 查询时,下一个组件的更改状态显示为 "success"

javascript - 如何使工厂中更新的值显示在DOM中