add env var checking

This commit is contained in:
Guillaume Dorce 2022-08-26 09:57:34 +02:00
parent 001cce5374
commit ece8da877a
2 changed files with 23 additions and 2 deletions

View File

@ -1,2 +1,4 @@
DATABASE_URL='<database_url>'
SECRET_KEY='<secret_key>'
DB_URL='<database_url>'
JWT_SECRET='<secret_key>'
JWT_EXPIRES_IN='1d'
PORT=5000

View File

@ -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;