chore(server): 优化环境变量加载与配置
1. 更新.gitignore,忽略所有.env文件但保留.env.example 2. 新增dotenv和cross-env依赖,完善环境变量加载逻辑 3. 新增dev:local本地开发启动脚本,适配多环境配置加载 4. 调整start启动脚本,统一使用cross-env处理环境变量
This commit is contained in:
1 parent
219091f864
commit
609221f544
5 files changed
+8674
-712
No files matched your search
@@ -1,5 +1,19 @@
|
||||
import { MidwayConfig } from "@midwayjs/core";
|
||||
import { existsSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { config as loadEnv } from "dotenv";
|
||||
|
||||
// 加载项目根目录的环境变量文件:
|
||||
// 1) .env 基础配置(默认=生产,不覆盖系统环境变量)
|
||||
// 2) .env.{环境} 环境专属覆盖,如 .env.local(MIDWAY_SERVER_ENV=local 时生效)
|
||||
const envDir = join(__dirname, "../../");
|
||||
loadEnv({ path: join(envDir, ".env") });
|
||||
|
||||
const currentEnv = process.env.MIDWAY_SERVER_ENV || process.env.NODE_ENV || "prod";
|
||||
const envFile = join(envDir, `.env.${currentEnv}`);
|
||||
if (existsSync(envFile)) {
|
||||
loadEnv({ path: envFile, override: true });
|
||||
}
|
||||
|
||||
export default {
|
||||
keys: "smart-grid-iot-secret-key",
|
||||
|
||||
Reference in new issue
Block a user