简体   繁体   English

如何流式传输API响应而不是将其全部加载到RAM

[英]How to stream an API response rather than load it all into RAM

I have an API that presents a large data set. 我有一个提供大量数据的API。 Initially, this data set was manageable, but it grows every day and is starting to bog down the server and is causing timeouts, and slow response. 最初,该数据集是可管理的,但它每天都在增长,并开始陷入服务器瘫痪,并导致超时和响应缓慢。 Currently, the API loads the entire response into memory before sending it to the client. 当前,API将整个响应加载到内存中,然后再发送给客户端。 I'd like to stream the response instead as soon as it is able to. 我想尽快发送响应。

I followed an online guide on how to construct this API, from here: https://www.freecodecamp.org/news/how-to-build-blazing-fast-rest-apis-with-node-js-mongodb-fastify-and-swagger-114e062db0c9/ 我从这里遵循了有关如何构造此API的在线指南: https : //www.freecodecamp.org/news/how-to-build-blazing-fast-rest-apis-with-node-js-mongodb-fastify -and-swagger-114e062db0c9 /

I modified it to suit my data, and it worked well enough. 我对其进行了修改以适合我的数据,并且效果很好。 However, I've been trying to get it to stream the response now, and I've frankly come up against a wall. 但是,我一直在尝试使其流式传输响应,并且坦率地碰壁。 I can't seem to adopt the many, many examples online of how to do this into my code. 我似乎无法在网上采用很多示例,说明如何在我的代码中执行此操作。

This is what I am trying to change. 这就是我要改变的。

// Get All Metars
exports.getmetars = async (req, reply) => {
  try {
    const flights = await metars.find()
    return flights
  } catch (err) {
    throw boom.boomify(err)
  }
}

This code currently loads the entirety into RAM before sending. 当前,此代码在发送之前将全部加载到RAM中。 How do I stream it instead? 我该如何流式传输?

You can stream but you will likely also want to look at enabling pagination, filtering, and/or sorting on your endpoint. 您可以流式传输,但是您可能还希望查看在端点上启用分页,过滤和/或排序。 Based on the article you linked I'm going to assume you're using Mongoose. 根据您链接的文章,我将假设您使用的是Mongoose。

Have a look at this streaming JSON.stringify package . 看一下这个流JSON.stringify包 There's a Mongoose example at the bottom of the readme. 自述文件底部有一个Mongoose示例 For you it'd look something like this: 对于您来说,它看起来像这样:

// Get All Metars
exports.getmetars = (req, reply) => {
  try {
    new JsonStreamStringify(metars.find().stream()).pipe(reply)
  } catch (err) {
    throw boom.boomify(err)
  }
}

Referenced answer: https://stackoverflow.com/a/53852868/30697 引用的答案: https : //stackoverflow.com/a/53852868/30697

Also see: Stream from a mongodb cursor to Express response in node.js 另请参阅: 从mongodb游标流到node.js中的Express响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过API将Twilio语音响应设置为URL,而不是TwiML App - Set Twilio Voice Response to URL rather than TwiML App via API 如何从 stream 而不是磁盘 Node.js 复制和 zip 文件夹 - How to copy and zip folder from stream rather than disk- Node.js Express.JS:如何按名称而不是数字设置响应状态? - Express.JS: how can I set response status by name rather than number? 如何从Node.js中的API请求创建响应流? - How to create a stream of response from an API request in Node.js? 如何使用语义UI和JQuery使特定卡片上的调光器而不是我的所有卡片库切换 - How to get dimmer on specific card to toggle rather than all my card gallery using Semantic UI and JQuery Azure功能:NodeJS-HTTP响应呈现为XML而不是HTML - Azure Functions: NodeJS - HTTP Response Renders as XML rather than HTML 如何从 API 响应中部分初始化具有大量数据的数组 - How to partially initialize an array with huge load of data from API response 我可以导入babel-polyfill模块而不是全部吗? - Can I import babel-polyfill modules rather than all in? Azure函数:NodeJS-HTTP响应呈现为XML而不是HTTP响应 - Azure Functions: NodeJS - HTTP Response Renders as XML Rather than HTTP Response 如何结束对 Twitter 流 API 的 npm oauth.get 请求的流响应? - How to end the streaming response to an npm oauth.get request to Twitter stream API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM