vue(html)中导入js文件
index.html:
config_settings.js:
注意:这里除了用window.config_settings,还可以用let config_settings变量的方式,该变量会自动挂载在window上。
使用时可直接调用config_settings对象。
打包后,public中的文件直接是在根目录下:
js中导入导出js
module.exports导出对象
require导入对象
注意:这里publicPath设置值后,可通过process.env.BASE_URL拿到
koa2中导入外部配置json文件,可直接使用
const fs = require("fs");
const path = require("path");
// const configPath = path.join(process.execPath, "../", "./config.json"); //也可以
const configPath = path.join(process.cwd(), "./config_node.json");
let isConfigExist = fs.existsSync(configPath);
let jsonConfig = null
if (isConfigExist) {
jsonConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
}
process.env.PORT = jsonConfig.port
console.log(`服务地址:http://${jsonConfig.host}:${jsonConfig.port}`)