Express middleware to combine multiple API requests into one.
const express = require('express');
const bunch = require('express-bunch-middleware');
const app = express();
app.use('/resources', bunch);
app.get('/api/users', (req, res) => {
res.json([{name: 'John', surname: 'Doe'}]);
});
app.get('/api/countries/:id', (req, res) => {
setTimeout(() => {
res.json({name: 'United States of America', countryCode: 'USA'});
}, 30)
});
app.listen(3000);
API call like:
GET /resources?users=/api/users&country=/api/countries/23
will responds with:
{
"users": [{
"name": "John",
"surname": "Doe"
}],
"countries": [{
"name": "United States of America",
"countryCode": "USA"
}]
}
MIT