简体   繁体   中英

How to optimize api response in nodejs?

I have list of articles. they contain category and sub-category.

const articles = [ { name, ..., category: { name, … , subCategories: [{ name, ... }] } } ]

I have api endpoint that I send all my articles with category and subCategories (it is mandatory).

The problem is category and subcategories are repeats (and it can be big data to send), the same category and subcategory is in article1 and article90 for example.

I think about to exclude category from the article, just replace with an id and create another property category, and send them back to the client.

res.json({ categories: [{ name: "1", ... , subCategories }], articles: [{ name , …, category: "1" }] });

How to that in easy way and generic for all my objects that repeat data in nodejs?

I think depends on the what you application need to display. But a common solution is using pagination (eg, getting chunks of 10 unities) in the API and, then, handling the pagination in the frontend.

Other solution is having some kind of consolidation. For example, if you would list all reads of a device in a interval, that sends data to server every 5 seconds, you could run a background service to consolidate data and save a mean of all values. Then, the frontend would get the mean at the interval, not actually all reads .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM