Complete the code to create a basic API gateway route in Express.
app.[1]('/api/service', (req, res) => { res.send('Service response'); });
The get method defines a route for HTTP GET requests, which is typical for API gateway endpoints.
Complete the code to forward the client request to the backend service URL.
const response = await fetch('[1]' + req.url);
The API gateway forwards requests to the backend service URL, which is typically an HTTP(S) endpoint like https://api.example.com.
Fix the error in the code to correctly proxy the request headers.
const backendResponse = await fetch(backendUrl, { headers: req.[1] });The headers property contains the HTTP headers from the client request, which should be forwarded to the backend.
Fill both blanks to correctly send the backend response status and body to the client.
res.status(backendResponse.[1]).[2](await backendResponse.text());
The status method sets the HTTP status code, and send sends the response body as text.
Fill all three blanks to implement a simple API gateway middleware that logs requests, forwards them, and sends back the response.
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());
});The middleware logs the request URL (req.url), forwards the request to the backend service URL (https://backend.service), and forwards the request headers (headers).