繁体   English   中英

如何在HTML文件中获取节点配置

[英]How to get the node config in html file

我根据环境有一个节点配置。 https://github.com/lorenwest/node-config

default.json

{
  "server": {
    "host": "localhost",
    "protocol": "http",
    "port": 9011
  }
}

我想在<script>标记内的index.html获得它。 有谁知道该怎么做?

我可以在js文件中获得此配置,如下所示

app.js

var config = require('config');
console.log(config.server.host);

对app.js文件进行Ajax调用,获取配置文件数据并在index.html文件中使用

我是node-config的维护者。 在复杂的应用程序中,您可能会有一些只希望在后端可见的秘密,以及一些您希望与前端共享的值。

将要与前端共享的所有值放在frontend config键下。

然后在/config.js创建一个服务于frontend配置的express路由:

router.get('/config.js', _configjs);
// Cache the config, don't recompute it on every request
var configJavascript = 'window.CONFIG='+JSON.stringify(config.get('frontend'));
function _configjs (req, res) {
  res.setHeader("Content-Type", "text/javascript");
  // Last modified now
  res.setHeader('Last-Modified', (new Date()).toUTCString());
  // Cache the config for 5 minutes
  res.setHeader('Cache-Control', 'max-age='+(60*5));
  res.writeHead(200);
  res.end(configJavascript);
}

现在,从前端加载/config.js之后,您可以通过以下方式访问前端配置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM