I don't know how to ask this question, so I will describe what I'm doing, and I'd like some enlightenment.
I'm using node
with express
for server-side rendering.
When I'm developing the web app, there's a module holding a variable which is shared across all the express routes, instead of hard-coding it.
config.js
module.exports = {
ipAddress: "http://localhost:8080"
}
index.js route
var config = require("../../config.js");
router
.get('/', function (req, res) {
var html = `
<div class="content">
<a href="${config.ipAddress}/orders">Orders</a>
<a href="${config.ipAddress}/invoices">Invoices</a>
</div>
`
res.send(html);
});
module.exports = router;
When it's time to deploy the app, I manually change the config.js
into the server's IP address:
module.exports = {
ipAddress: "http://255.255.255.255"
}
Another example is the scripts.js
file referenced in the HTML head, which handles the DOM
changes. Again, the server IP address is placed in the variable.
scripts.js
var ipAddress = "http://localhost:8080"; // To be changed into "http://255.255.255.255"
function placeOrder() {
fetch(`${ipAddress}/place-order`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-type": "application/json"
}
})
.then((res) => res.json())
.then((data) => xonsole.log(data))
.catch((err) => console.log(err));
}
document.getElementById("submit-order").addEventListener("click", () => placeOrder());
What is a proper way to handle these variable changes automatically?