How do I build a feed API in Node.js?
GET /feed?skip=&limit=. Exclude the current user and already-swiped users. Cap the limit. Use .lean() and .select(). Return total. Run find and countDocuments in parallel with Promise.all. Index the filter fields.
Explore This Question
Search for additional explanations using these tools:
More FAQs in How to Build a Feed API in Node.js
Fetch the swiped user IDs with ConnectionRequest.find({ fromUserId }).distinct('toUserId'). Then filter the feed with _id: { $nin: [req.user.userId, ...swipedIds] }. Excludes the current user and anyone already swiped on.
To prevent abuse. Without a cap, a client can request 10000 items, eating memory and bandwidth. Use Math.min(parseInt(req.query.limit) || 20, 100). Most UIs only show 20 at a time anyway.
So the client can show 'showing 1-20 of 432' and decide whether to show a next button. Use Promise.all([find, countDocuments]) to run both queries in parallel and avoid waterfall delays.
Still have questions?
Browse all our FAQs or reach out to our support team
