JAVASCRIPT

Backend API Data Aggregation from Multiple Internal Microservices

Build a Node.js Express endpoint to aggregate data by making concurrent requests to several internal microservices, consolidating and presenting a unified response to the client efficiently.

const express = require('express');
const fetch = require('node-fetch'); // Or use axios
const app = express();
const PORT = 3000;

// Assume these are internal microservice URLs
const USER_SERVICE_URL = 'http://localhost:3001/users';
const PRODUCT_SERVICE_URL = 'http://localhost:3002/products';
const ORDER_SERVICE_URL = 'http://localhost:3003/orders';

app.get('/dashboard-data/:userId', async (req, res) => {
  const { userId } = req.params;

  try {
    // Concurrently fetch data from multiple microservices
    const [userDataResponse, productsDataResponse, ordersDataResponse] = await Promise.all([
      fetch(`${USER_SERVICE_URL}/${userId}`),
      fetch(`${PRODUCT_SERVICE_URL}?userId=${userId}`),
      fetch(`${ORDER_SERVICE_URL}?userId=${userId}`)
    ]);

    // Check if any of the responses were not OK
    const responses = [userDataResponse, productsDataResponse, ordersDataResponse];
    const failedResponse = responses.find(response => !response.ok);
    if (failedResponse) {
      const errorText = await failedResponse.text();
      console.error(`Microservice error: ${failedResponse.status} - ${errorText}`);
      return res.status(500).json({ error: 'Failed to retrieve data from one or more services', details: errorText });
    }

    const userData = await userDataResponse.json();
    const productsData = await productsDataResponse.json();
    const ordersData = await ordersDataResponse.json();

    // Aggregate the data into a single response
    const aggregatedData = {
      user: userData,
      products: productsData,
      orders: ordersData,
      timestamp: new Date().toISOString()
    };

    res.json(aggregatedData);
  } catch (error) {
    console.error('Error during data aggregation:', error);
    res.status(500).json({ error: 'Internal server error during data aggregation' });
  }
});

app.listen(PORT, () => {
  console.log(`Aggregation service running on http://localhost:${PORT}`);
});
How it works: This Node.js Express snippet demonstrates a common backend pattern for data aggregation. A single API endpoint receives a request (e.g., for user dashboard data) and then concurrently fetches information from several internal microservices using `Promise.all`. After retrieving and awaiting all individual service responses, it combines and formats this data into a unified JSON object, which is then sent back to the client. This approach helps in building a cohesive API from a distributed microservice architecture.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs