0

I have a project (or 2 actually) where a frontend server is talking/requesting data from a backend server (written in Python). Now when I build and distribute the dist I also define the ip-address to the backend. This is not dynamic and i have to change ip and build again for every distribution (new IP-address). My wish is to be able to set the ip-address to backend-server, if default coded address is wrong (usually) via a data.json file that is looking like this

[{"FRONTEND_IP": "192.168.1.35" },{"BACKEND_IP": "192.168.1.123" }]

There is a ton of samples on how to read .json files from within code base and I have discovered that i can import file into a variable, like this

import posts from '../../data.json'

And use it as the array of json data it is. My problem is that when I build with

npm run build

it (the data.json file) gets included into the dist package hence I can't change the IP.

Have you got any tips on how to solve my problem with an external config file outside the dist compilation

/a

1 Answer 1

1

You can read the source IP from a txt file so that you can change it whenever you want.

const fs = require('fs');

fs.readFile('./data.json', function read(err, data) {
if (err) {
    throw err;
}
const content = data;

loadIp(content);
});

function loadIp(content) {
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sabri, tried with your advice as this ''' onMounted(async () => { console.log("onMounted ") console.log("Backend:", BACKEND_PATH.value) const fs = require('fs'); fs.readFile("./posts.json", function read(err, data){ console.log("read error", err) console.log("data", data) }); }); but got a bunch of errors, main is "TypeError: e.readFile is not a function"
Found a combined solution to above question. Exclude in vue.config.js and read as either import (my test) or as Sabri says. Exclude like this stackoverflow.com/questions/53035036/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.