何时在nodejs应用程序中加载.env变量?

tjjdgumg  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(306)

我正在使用typescript编写一个简单的nodejs express rest api。我加载了一些环境变量 dotenv .
我访问我的 .env 代码中两个不同阶段的变量: index.ts ,这是我的开始文件,在 MyControllerClass.ts 文件要访问这些变量,代码是 process.env.MY_ENV_VAR . 要为应用程序加载它们,代码如下 dotenv.config() .
作为我的 index.ts 文件似乎是我的程序的根(我在其中配置我的应用程序),我使用 dotenv.config() 加载我的 .env 程序其余部分的文件。但是在我的 MyControllerClass.ts 文件,在构造函数中,如果我这样做 console.log(process.env.MY_ENV_VAR) “我得到” undefined ". 我可以通过添加一个 dotenv.config() 在我的构造器中(它可以工作),但在这里使用它对我来说毫无意义。
我如何使用 dotenv.config() 在我的程序中,以可读的方式(如在适当的.ts文件中)一劳永逸?更一般地说:什么是nodejs express加载周期?
下面是我的代码的文件结构示例

src
├── index.ts
├── Authentication
│   └── authentication.router.ts
│   └── authentication.controller.ts

下面是index.js的代码

/**
 * Required External Modules
 */
 import * as dotenv from "dotenv";
 import express from "express";
 import cors from "cors";
 import helmet from "helmet";
 import { authenticationRouter } from "./authentication/authentication.router"

 dotenv.config();

 /**
 * App Variables
 */
 if(!process.env.PORT) {
    process.exit(1);
}
const PORT: number = parseInt(process.env.PORT as string, 10);
const app = express();

/**
 *  App Configuration
 */
 app.use(helmet());
 app.use(cors());
 app.use(express.json());
 app.use(authenticationRouter);

 app.use("api/authenticate/", authenticationRouter);
/**
 * Server Activation
 */
 app.listen(PORT, () => {
    console.log(`Listening on port ${PORT}`);
  });

这是authentication.router.ts的代码

import express, { Request, Response } from "express";
import { AuthenticatorController } from "./authentication.controller";
export const authenticationRouter = express.Router();
const authenticatorController = AuthenticatorController.getInstance();

authenticationRouter.post("/api/authenticate", async (req: Request, res: Response) => {   
    try {
        if (await authenticatorController.authenticate(req.body.login, req.body.password)) {
            res.send({"status": "ok"})
        } else
            res.send({"status": "Error"})
    } catch (e) {
        console.debug(e)
        res.send({"status": "500"});
    }

});

这是authentication.controller.ts的代码

import { ClientSecretCredential } from "@azure/identity";
import { SecretClient } from "@azure/keyvault-secrets";
import { Authenticator } from "./api/Authenticator";
import * as dotenv from "dotenv";
dotenv.config();

export class AuthenticatorController implements Authenticator {

    private static singleInstance: AuthenticatorController | null = null;
    private azureSecretCredential= new ClientSecretCredential(
        process.env.AZURE_TENANT_ID as string,
        process.env.AZURE_CLIENT_ID as string,
        process.env.AZURE_CLIENT_SECRET as string);
    private azureSecretClient = new SecretClient(
        process.env.KEY_VAULT_URL as string,
        this.azureSecretCredential);

    private constructor () {}

    public static getInstance(): AuthenticatorController {
        if (this.singleInstance === null) {
            this.singleInstance = new AuthenticatorController();
        }
        return this.singleInstance;
    }

    public async authenticate(login: string, password: string): Promise<Boolean> {
        let isAuthenticated = false;

        try {
            const secret = await this.azureSecretClient.getSecret(login)
            if (secret.name === login) {
                if (secret.value === password) {
                    isAuthenticated = true;
                }
            }
        }   catch (e) {
            console.debug(e);
        }
            return isAuthenticated;
    }
}
oknrviil

oknrviil1#

你只需要打电话 dotenv.config() 一次:
在应用程序中尽早要求并配置dotenv。 require('dotenv').config() 因此index.ts似乎是正确的, process.env 然后应该保存解析后的值。也许您可以使用类似的方法来确保正确解析数据:

const result = dotenv.config();

if (result.error) {
  throw result.error;
}

console.log(result.parsed);

编辑:
您可以尝试以下方法。我稍微更改了导出,因为在控制器中不需要单例。
authentication.router.ts:

// Imports (no dotenv; no dotenv.config())
// [...]

// Import controller
import { authenticatorController } from "./authentication.controller";

export const authenticationRouter = express.Router();

// Adding routes
// [...]

authentication.controller.ts:

// Imports (no dotenv; no dotenv.config())
// [...]

class AuthenticatorController implements Authenticator {
  // [...]
}

export const authenticatorController = new AuthenticatorController();

index.ts:

// Imports (dotenv)
// [...]

const { error, parsed } = dotenv.config();

if (error) {
  throw error;
}

console.log(parsed);

// [...]

 app.use("api/authenticate/", authenticationRouter);

// [...]

相关问题