Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dotenv for configuration management
- Loading branch information
1 parent
3e520a8
commit 7e42081
Showing
16 changed files
with
71 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
NODE_ENV=development | ||
PORT=4040 | ||
JWT_SECRET=0a6b944d-d2fb-46fc-a85e-0295c986cd9f | ||
MONGO_HOST=mongodb://localhost/express-mongoose-es6-rest-api-development | ||
MONGO_PORT=27017 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,6 @@ node_modules | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# .env | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Joi from 'joi'; | ||
|
||
// require and configure dotenv, will load vars in .env in PROCESS.ENV | ||
require('dotenv').config(); | ||
|
||
// define validation for all the env vars | ||
const envVarsSchema = Joi.object({ | ||
NODE_ENV: Joi.string() | ||
.allow(['development', 'production', 'test', 'provision']) | ||
.default('development'), | ||
PORT: Joi.number() | ||
.default(4040), | ||
MONGOOSE_DEBUG: Joi.boolean() | ||
.when('NODE_ENV', { | ||
is: Joi.string().equal('development'), | ||
then: Joi.boolean().default(true), | ||
otherwise: Joi.boolean().default(false) | ||
}), | ||
JWT_SECRET: Joi.string().required() | ||
.description('JWT Secret required to sign'), | ||
MONGO_HOST: Joi.string().required() | ||
.description('Mongo DB host url'), | ||
MONGO_PORT: Joi.number() | ||
.default(27017) | ||
}).unknown() | ||
.required(); | ||
|
||
const { error, value: envVars } = Joi.validate(process.env, envVarsSchema); | ||
if (error) { | ||
throw new Error(`Config validation error: ${error.message}`); | ||
} | ||
|
||
const config = { | ||
env: envVars.NODE_ENV, | ||
port: envVars.PORT, | ||
mongooseDebug: envVars.MONGOOSE_DEBUG, | ||
jwtSecret: envVars.JWT_SECRET, | ||
mongo: { | ||
host: envVars.MONGO_HOST, | ||
port: envVars.MONGO_PORT | ||
} | ||
}; | ||
|
||
export default config; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters