Secret Data
To Prevent sensitive data (e.g. Database PW, Connect String, Deploy Key) included in the source code directly, we may need to import those value by enviromment variables.
Also, we may use this method to enhance the reuse of several variables.
Install & Usage
Install the following package
yarn add dotenv
Create a .env
file at you root
.env
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
PORT=5678
server.ts
import 'dotenv/config' // Require this to active ENV
import express, { Request, Response } from "express";
const app = express();
// Using `process.env` to import
const mySecret = process.env.SECRET_KEY;
app.get("/", function (req: Request, res: Response) {
res.end("Hello mom!");
});
const PORT = process.env.PORT ? +process.env.PORT : 8181;
app.listen(PORT, () => {
console.log(`Listening at http://localhost:${PORT}/`);
});