可以通过以下代码示例解决:
const limiter = rateLimit({ windowMs: 60 * 1000, // 1 minute max: 100 // limit each IP to 100 requests per windowMs });
app.use(limiter);
async function handleRequest(req, res) { try { // 使用Promise.all()并发请求 const responses = await Promise.all([ fetch('/api/users'), fetch('/api/products'), fetch('/api/orders') ]); const users = await responses[0].json(); const products = await responses[1].json(); const orders = await responses[2].json(); // 处理请求结果 res.json({ users, products, orders }); } catch (err) { // 处理错误信息 console.error(err); res.status(500).json({ message: 'Internal Server Error' }); } }
app.get('/api/data', handleRequest);
下一篇:并发任务的动态限制