From ece8da877a5eea671c40254634fdc1f4e973ae2a Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Fri, 26 Aug 2022 09:57:34 +0200 Subject: [PATCH] add env var checking --- .env.example | 6 ++++-- src/index.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index b271385..c4bdcc1 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,4 @@ -DATABASE_URL='' -SECRET_KEY='' \ No newline at end of file +DB_URL='' +JWT_SECRET='' +JWT_EXPIRES_IN='1d' +PORT=5000 diff --git a/src/index.ts b/src/index.ts index 80b442b..6f0f211 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,25 @@ import express, { urlencoded, json } from 'express'; import cors from 'cors'; import api from '@/api'; +import { config } from 'dotenv'; + +config(); + +const checkEnvVars = () => { + const requiredEnvVars = ['PORT', 'DB_URL', 'JWT_SECRET', 'JWT_EXPIRES_IN']; + let error: Boolean = false; + requiredEnvVars.forEach((envVar) => { + if (process.env[envVar] === undefined) { + error = true; + console.log(`${envVar} is undefined`); + } + }); + if (error) { + process.exit(1); + } +}; + +checkEnvVars(); const port = process.env.PORT || 5000;