我不知道如何问这个问题,所以我将描述我在做什么,我希望得到一些启发。
我正在使用node
withexpress
进行服务器端渲染。
当我开发 Web 应用程序时,有一个模块包含一个变量,该变量在所有快速路由之间共享,而不是对其进行硬编码。
配置.js
module.exports = {
ipAddress: "http://localhost:8080"
}
index.js路由
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;
部署应用程序时,我手动将其更改config.js
为服务器的 IP 地址:
module.exports = {
ipAddress: "http://255.255.255.255"
}
另一个例子是scripts.js
HTML 头中引用的文件,它处理DOM
更改。同样,服务器 IP 地址被放置在变量中。
脚本.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());
自动处理这些变量更改的正确方法是什么?