Bird
Raised Fist0
Expressframework~10 mins

Microservice communication basics in Express - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic Express server that listens on port 3000.

Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from service!');
});

app.listen([1], () => {
  console.log('Server is running');
});
Drag options to blanks, or click blank then click option'
A5000
B8080
C3000
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using port 80 without admin rights
Using a port already in use
2fill in blank
medium

Complete the code to send a GET request from one microservice to another using the 'fetch' API.

Express
const fetch = require('node-fetch');

async function getData() {
  const response = await fetch('[1]');
  const data = await response.json();
  return data;
}
Drag options to blanks, or click blank then click option'
A'http://localhost:3000/api/data'
B'file://localhost:3000/api/data'
C'ftp://localhost:3000/api/data'
D'localhost:3000/api/data'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the protocol
Using wrong protocols like ftp or file
3fill in blank
hard

Fix the error in the Express route to correctly parse JSON request bodies.

Express
const express = require('express');
const app = express();

app.use([1]);

app.post('/data', (req, res) => {
  res.json({ received: req.body });
});
Drag options to blanks, or click blank then click option'
Aexpress.text()
Bexpress.urlencoded()
Cexpress.raw()
Dexpress.json()
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() for JSON
Not using any body parser
4fill in blank
hard

Fill both blanks to create an Express route that calls another microservice and returns its data.

Express
const fetch = require('node-fetch');
const express = require('express');
const app = express();

app.get('/proxy', async (req, res) => {
  const response = await fetch([1]);
  const data = await response.[2]();
  res.json(data);
});
Drag options to blanks, or click blank then click option'
A'http://localhost:4000/data'
B'http://localhost:4000/info'
Cjson
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong URL
Using response.text() when expecting JSON
5fill in blank
hard

Fill all three blanks to implement a retry mechanism when calling another microservice.

Express
const fetch = require('node-fetch');

async function fetchWithRetry(url, retries = [1]) {
  for (let i = 0; i < retries; i++) {
    try {
      const response = await fetch(url);
      if (response.ok) {
        return await response.[2]();
      }
    } catch (error) {
      if (i === retries - 1) throw error;
    }
  }
}

const maxRetries = [3];
Drag options to blanks, or click blank then click option'
A3
Bjson
C5
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Parsing response as text
Setting retries too low or too high without reason

Practice

(1/5)
1. Which of the following is a common way for microservices to communicate synchronously in an Express-based system?
easy
A. Sharing global variables in memory
B. Direct database access by all services
C. Using HTTP REST API calls between services
D. Using local file system for message passing

Solution

  1. Step 1: Understand synchronous communication in microservices

    Synchronous communication means one service waits for a response from another before continuing.
  2. Step 2: Identify common synchronous methods in Express

    HTTP REST API calls are widely used for synchronous communication between microservices in Express.
  3. Final Answer:

    Using HTTP REST API calls between services -> Option C
  4. Quick Check:

    Synchronous communication = HTTP REST API calls [OK]
Hint: Sync communication usually uses HTTP requests [OK]
Common Mistakes:
  • Thinking direct DB access is common for microservices
  • Assuming shared memory works across services
  • Using file system for communication
2. Which Express code snippet correctly sends a JSON response from one microservice to another?
easy
A. res.json({ data: 'value' })
B. res.write({ data: 'value' })
C. res.send("{ data: 'value' }")
D. res.render({ data: 'value' })

Solution

  1. Step 1: Identify Express method for JSON response

    Express provides res.json() to send JSON formatted responses easily.
  2. Step 2: Compare other methods

    res.send() can send JSON but res.json() is preferred for clarity; res.write() is low-level and res.render() is for templates.
  3. Final Answer:

    res.json({ data: 'value' }) -> Option A
  4. Quick Check:

    Express JSON response = res.json() [OK]
Hint: Use res.json() to send JSON responses in Express [OK]
Common Mistakes:
  • Using res.send() without JSON stringification
  • Confusing res.render() with JSON response
  • Using res.write() incorrectly
3. Consider two microservices where Service A calls Service B using HTTP GET. What will Service A receive if Service B responds with res.status(200).json({ message: 'Success' })?
medium
A. A JSON object { message: 'Success' }
B. A plain text 'Success' string
C. An HTTP 404 error
D. An empty response

Solution

  1. Step 1: Understand Service B's response

    Service B sends HTTP status 200 with JSON body { message: 'Success' }.
  2. Step 2: Determine what Service A receives

    Service A receives the JSON object exactly as sent, since status 200 means OK and body is JSON.
  3. Final Answer:

    A JSON object { message: 'Success' } -> Option A
  4. Quick Check:

    HTTP 200 + res.json() = JSON object response [OK]
Hint: HTTP 200 with res.json() returns JSON object [OK]
Common Mistakes:
  • Assuming plain text instead of JSON
  • Confusing status codes with response body
  • Expecting empty or error response
4. In an Express microservice, the following code is used to call another service asynchronously. What is the main issue?
fetch('http://service-b/api/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error))
medium
A. Missing await keyword for fetch call
B. fetch is not defined in Node.js by default
C. No error handling for non-JSON responses
D. Using console.log instead of res.send

Solution

  1. Step 1: Check environment support for fetch

    Node.js versions before 18 do not have fetch globally defined by default.
  2. Step 2: Identify the main issue in Express microservice

    Without importing or enabling fetch, this code will cause a ReferenceError in Node.js.
  3. Final Answer:

    fetch is not defined in Node.js by default -> Option B
  4. Quick Check:

    Node.js needs fetch polyfill or version 18+ [OK]
Hint: Node.js needs fetch polyfill or v18+ for fetch [OK]
Common Mistakes:
  • Thinking await is mandatory with then() chains
  • Ignoring fetch availability in Node.js
  • Confusing console.log with response sending
5. You want to design communication between two Express microservices where Service A sends a task to Service B and does not wait for a response. Which approach is best to ensure reliable, scalable communication?
hard
A. Use WebSocket connection for real-time two-way communication
B. Use HTTP POST with immediate response from Service B
C. Use direct database writes by Service A for Service B to poll
D. Use a message queue like RabbitMQ for asynchronous messaging

Solution

  1. Step 1: Understand asynchronous communication needs

    Service A sends a task and does not wait, so async messaging is needed.
  2. Step 2: Evaluate options for reliability and scalability

    Message queues like RabbitMQ provide reliable, scalable async communication with delivery guarantees.
  3. Final Answer:

    Use a message queue like RabbitMQ for asynchronous messaging -> Option D
  4. Quick Check:

    Async + reliable + scalable = message queue [OK]
Hint: Async tasks use message queues for reliability [OK]
Common Mistakes:
  • Using HTTP POST expecting async without waiting
  • Relying on database polling which is inefficient
  • Using WebSocket for one-way async tasks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard