[英]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.