Skip to content

Commit 29aa707

Browse files
authored
Merge pull request #307 from KunalKapadia/develop
Develop to Master
2 parents 069cc48 + d5cd966 commit 29aa707

File tree

16 files changed

+73
-48
lines changed

16 files changed

+73
-48
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
NODE_ENV=development
2+
PORT=4040
3+
JWT_SECRET=0a6b944d-d2fb-46fc-a85e-0295c986cd9f
4+
MONGO_HOST=mongodb://localhost/express-mongoose-es6-rest-api-development
5+
MONGO_PORT=27017

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ node_modules
4646

4747
# Optional REPL history
4848
.node_repl_history
49+
50+
# .env
51+
.env

.istanbul.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
verbose: false
22
instrumentation:
3-
excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js', 'config/env/development.js', 'config/env/production.js']
3+
excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js']
44
include-all-sources: true
55
reporting:
66
print: summary
@@ -22,4 +22,4 @@ check:
2222
statements: 50
2323
lines: 50
2424
branches: 30
25-
functions: 20
25+
functions: 20

config/config.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Joi from 'joi';
2+
3+
// require and configure dotenv, will load vars in .env in PROCESS.ENV
4+
require('dotenv').config();
5+
6+
// define validation for all the env vars
7+
const envVarsSchema = Joi.object({
8+
NODE_ENV: Joi.string()
9+
.allow(['development', 'production', 'test', 'provision'])
10+
.default('development'),
11+
PORT: Joi.number()
12+
.default(4040),
13+
MONGOOSE_DEBUG: Joi.boolean()
14+
.when('NODE_ENV', {
15+
is: Joi.string().equal('development'),
16+
then: Joi.boolean().default(true),
17+
otherwise: Joi.boolean().default(false)
18+
}),
19+
JWT_SECRET: Joi.string().required()
20+
.description('JWT Secret required to sign'),
21+
MONGO_HOST: Joi.string().required()
22+
.description('Mongo DB host url'),
23+
MONGO_PORT: Joi.number()
24+
.default(27017)
25+
}).unknown()
26+
.required();
27+
28+
const { error, value: envVars } = Joi.validate(process.env, envVarsSchema);
29+
if (error) {
30+
throw new Error(`Config validation error: ${error.message}`);
31+
}
32+
33+
const config = {
34+
env: envVars.NODE_ENV,
35+
port: envVars.PORT,
36+
mongooseDebug: envVars.MONGOOSE_DEBUG,
37+
jwtSecret: envVars.JWT_SECRET,
38+
mongo: {
39+
host: envVars.MONGO_HOST,
40+
port: envVars.MONGO_PORT
41+
}
42+
};
43+
44+
export default config;

config/env/development.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

config/env/index.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

config/env/production.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

config/env/test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

config/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import expressValidation from 'express-validation';
1111
import helmet from 'helmet';
1212
import winstonInstance from './winston';
1313
import routes from '../server/routes/index.route';
14-
import config from './env';
14+
import config from './config';
1515
import APIError from '../server/helpers/APIError';
1616

1717
const app = express();

gulpfile.babel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const plugins = gulpLoadPlugins();
88

99
const paths = {
1010
js: ['./**/*.js', '!dist/**', '!node_modules/**', '!coverage/**'],
11-
nonJs: ['./package.json', './.gitignore'],
11+
nonJs: ['./package.json', './.gitignore', './.env'],
1212
tests: './server/tests/*.js'
1313
};
1414

1515
// Clean up dist and coverage directory
1616
gulp.task('clean', () =>
17-
del.sync(['dist/**', 'coverage/**', '!dist', '!coverage'])
17+
del.sync(['dist/**', 'dist/.*', 'coverage/**', '!dist', '!coverage'])
1818
);
1919

2020
// Copy non-js files to dist

0 commit comments

Comments
 (0)