Skip to content

Instantly share code, notes, and snippets.

@muhummadPatel
Forked from leafriend/example.ts
Created December 8, 2020 08:12
Show Gist options
  • Select an option

  • Save muhummadPatel/8c7c2d1d441bf0d588b754d435909a80 to your computer and use it in GitHub Desktop.

Select an option

Save muhummadPatel/8c7c2d1d441bf0d588b754d435909a80 to your computer and use it in GitHub Desktop.
Client-side Load Balancing with Axios
import axios, { AxiosRequestConfig } from 'axios';
(async () => {
const config: AxiosRequestConfig = {
get baseURL() {
let baseURL = 'https://example.com';
// Select node here
return baseURL;
},
};
const response = await axios.get('/path/to/api', config);
console.log(response.data);
})();
@muhummadPatel
Copy link
Copy Markdown
Author

Might actually be better to use a request interceptor to dynamically set the baseUrl (see https://github.com/axios/axios#interceptors)

axios.interceptors.request.use(function (config) {
    // Pick an endpoint and set config.baseUrl here
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment