0
0
Expressframework~10 mins

API gateway concept in Express - Interactive Code Practice

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

Complete the code to create a basic API gateway route in Express.

Express
app.[1]('/api/service', (req, res) => { res.send('Service response'); });
Drag options to blanks, or click blank then click option'
Aget
Blisten
Cuse
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.listen instead of app.get
Using app.use which is for middleware, not route handling
2fill in blank
medium

Complete the code to forward the client request to the backend service URL.

Express
const response = await fetch('[1]' + req.url);
Drag options to blanks, or click blank then click option'
Ahttps://api.example.com
Bhttp://localhost:3000
Cftp://backend.service
Dfile://local/path
Attempts:
3 left
💡 Hint
Common Mistakes
Using FTP or file protocol instead of HTTP/HTTPS
Using localhost which is not scalable for production
3fill in blank
hard

Fix the error in the code to correctly proxy the request headers.

Express
const backendResponse = await fetch(backendUrl, { headers: req.[1] });
Drag options to blanks, or click blank then click option'
Abody
Bparams
Cheaders
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using body instead of headers
Using params or query which are for URL parameters
4fill in blank
hard

Fill both blanks to correctly send the backend response status and body to the client.

Express
res.status(backendResponse.[1]).[2](await backendResponse.text());
Drag options to blanks, or click blank then click option'
Astatus
Bsend
Cjson
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using json() when the response is plain text
Using body property instead of status for status code
5fill in blank
hard

Fill all three blanks to implement a simple API gateway middleware that logs requests, forwards them, and sends back the response.

Express
app.use(async (req, res) => {
  console.log('Request to:', [1]);
  const backendResponse = await fetch([2] + req.url, { headers: req.[3] });
  res.status(backendResponse.status).send(await backendResponse.text());
});
Drag options to blanks, or click blank then click option'
Areq.url
Bhttps://backend.service
Cheaders
Dreq.body
Attempts:
3 left
💡 Hint
Common Mistakes
Logging req.body instead of req.url
Using incorrect backend URL format
Forwarding body instead of headers