0

I need to read from a json file saved in C:/ the ip address of the API server inside an electron-react app build for prod. This for don't build again the project for production when the ip changes.

Im using axios instance to make the calls to the api

import axios from 'axios';

const apiTest = 'http://localhost:8000/api';
const apiURL = 'http://189.173.109.69:1080/api'; // <- i need to get this from a json file saved in C drive

class AxiosInstance {
  private static instance: any;

  public manager: any;

  constructor() {
    if (AxiosInstance.instance) {
      return AxiosInstance.instance;
    }
    this.manager = axios.create({
      baseURL: apiURL,
    });



    this.manager.defaults.headers = {
      'content-type': 'application/json',
      Accept: 'application/json',
    };

    AxiosInstance.instance = this;
    this.manager.defaults.timeout = 15000;
    this.manager.interceptors.request.use((request: any) =>
      AxiosInstance.requestHandler(request)
    );
    this.manager.interceptors.response.use(
      (response: any) => {
        return response;
      },
      async (error: any) => {
        // Do something with response error
        if (error?.response?.data?.error && error.response.status === 401) {
          const errorText = error?.response?.data?.error;
          console.log(errorText);
          return Promise.reject(error);
        }
        // Do something with response error
        if (error?.response?.data?.error && error.response.status === 422) {
          let errorText = '';
          // eslint-disable-next-line guard-for-in
          for (const key in error?.response?.data?.error) {
            errorText += `${error?.response?.data?.error[key]} \n`;
          }
          console.log(errorText);
        }
        return Promise.reject(error);
      }
    );
  }

  static requestHandler = async (request: any) => {
    // const jwt = await SecureStore.getItemAsync('jwt');
    // if (jwt) {
    request.headers['content-type'] = 'application/json';
    // request.headers.Authorization = `Bearer ${jwt}`;
    // }
    return request;
  };
}

export { apiURL, apiTest, AxiosInstance };

export default new AxiosInstance().manager;

I need to get the IP address from that json file and put it in the axios instance as the apiURL (baseURL) to make api calls.

GbRuiz
  • 1
  • 2
  • Read the file using 'fs' module of nodeJS in constructor and update the apiUrl from the file content. Hope this helps: https://nodejs.org/en/knowledge/file-system/how-to-read-files-in-nodejs/ – Nayeem Jahan Rafi Aug 25 '21 at 10:59
  • I already tried that. But I dont know why the url are setting like this when adding to axios instance manager baseURL : "'file:///C:/Users/pc/Desktop/comicx%20electron/comicx_drive_thru/src/%22http://189.173.109.69:80/api%22/menu/categories'" @NayeemJahanRafi – GbRuiz Aug 25 '21 at 11:12
  • Share the code you are using to read the file and also the content of the file you are reading the url from. – Nayeem Jahan Rafi Aug 25 '21 at 17:48

0 Answers0