concept request in category docker

appears as: request, Request, requests
Bootstrapping Microservices with Docker, Kubernetes and Terraform

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform.

Listing 4.3 - Updated video streaming microservice that forwards the request to the video storage microservice (chapter-4/example-2/video-streaming/src/index.js)
const express = require("express");
const http = require("http"); #A
 
const app = express();
 
const PORT = process.env.PORT;
const VIDEO_STORAGE_HOST = process.env.VIDEO_STORAGE_HOST; #B
const VIDEO_STORAGE_PORT = parseInt(process.env.VIDEO_STORAGE_PORT); #B
 
app.get("/video", (req, res) => {
    const forwardRequest = http.request( #C
        {
            host: VIDEO_STORAGE_HOST, #D
            port: VIDEO_STORAGE_PORT, #D
            path:'/video?path=SampleVideo_1280x720_1mb.mp4', #E
            method: 'GET', #F
            headers: req.headers #G
        },
        forwardResponse => { #H
            res.writeHeader(forwardResponse.statusCode, forwardResponse.headers); #I
            forwardResponse.pipe(res); #J
        } #I
    ); #C
   
    req.pipe(forwardRequest); #L
});
 
app.listen(PORT, () => {
    console.log(`Microservice online`);
});
 

Reading the Axios documentation tells me that the default timeout is infinity! That means by default an Axios request can literally go forever without being aborted. We definitely need to set the timeout for any requests we make with Axios.

You can set the timeout for each request, but that requires repeated effort. Fortunately with Axios we can set a default timeout for all requests as shown in listing 10.3.

Listing 10.3 - Setting a default timeout for HTTP requests with Axios
const axios = require("axios");
axios.defaults.timeout = 2500; #A