A developer creates an Axios factory function to implement configurable keep-alive agents, emphasizing loosely coupled code. The process shifts to the Node.js server side in main.ts, where keep-alive timeouts and request headers are tuned to optimize performance and prevent resource exhaustion during connection handling.
Length 9:05
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #axios #nodejs #factory pattern #keepalive #http #typescript #vscode #networking #server #timeout
0:00 All right, so let's go ahead and now create a factory, which is a 0:03 design pattern that allows us to encapsulate the logic 0:07 behind creating objects in a dedicated 0:11 function or class. In this case, we're gonna create a new 0:15 function to be able to return us an Axios instance that 0:19 is already pre-configured with our new keep alive agents. 0:23 So inside of resilient HTTP, let's create a new 0:27 axios.factory.ts file. 0:31 And in here, I'll go ahead and export function 0:34 createAxiosInstance. And now the 0:38 createAxiosInstance function, I want this to take in an 0:41 options object that is going to allow us to configure Axios 0:45 further, along with our keep alive settings 0:48 and some other Axios 0:49 specific options. 0:52 So for this, I'll go back to my resilient HTTP types, 0:56 and just like we did for keep alive options, I'll create a new interface to 1:00 encapsulate all of the HTTP module 1:03 options. And again, this is so that we can keep our application highly 1:07 configurable and swap out different values through something like 1:11 our environment variables, which keeps our application really 1:15 nicely loosely coupled and configurable, which is a good practice. 1:20 So in here, we're gonna accept a bunch of optional 1:22 parameters that Axios 1:24 can take in. First one will be the base URL in 1:28 case we have a base URL that all the requests 1:30 are going to be going to. 1:32 This will save us from having to provide the full path to our URL. 1:37 We also have a timeout milliseconds value that Axios takes in, which 1:41 is an important number that allows you to specify how long an 1:45 HTTP request should wait for a response back from the 1:49 server. 1:50 So here you can actually configure after a certain period of time, we throw an 1:54 error and this is a useful configuration option because you might wanna fail
The rest of the transcript is for members.
Sign in