mirror of
https://github.com/andwati/order-service.git
synced 2026-10-25 21:32:17 +00:00
feat: add environment configuration and required variables
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
NODE_ENV=
|
||||
PORT=
|
||||
MONGO_URI=
|
||||
JWT_SECRET=
|
||||
@@ -2,6 +2,7 @@
|
||||
"name": "order-service",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import dotenv from "dotenv";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
function requireEnv(key: string): string {
|
||||
const value = process.env[key];
|
||||
|
||||
if (!value) {
|
||||
throw new Error(`Environment variable ${key} is required but not set.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export const env = {
|
||||
nodeEnv: process.env.NODE_ENV ?? "development",
|
||||
port: Number(process.env.PORT) ?? 3000,
|
||||
|
||||
mongoUri: requireEnv("MONGO_URI"),
|
||||
|
||||
jwtSecret: {
|
||||
secret: requireEnv("JWT_SECRET"),
|
||||
expiresIn: process.env.JWT_EXPIRES_IN ?? "1h",
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user