From 1aa6e40da79d953a9948fc1f128db101bba24ff1 Mon Sep 17 00:00:00 2001 From: hananwm Date: Tue, 14 Feb 2017 18:23:14 +0200 Subject: [PATCH 1/6] Replacing deprecated ghooks with husky for pre-commit hooks --- package.json | 6 +-- yarn.lock | 108 ++++++++++++++------------------------------------- 2 files changed, 32 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index 74c4b994..1d568771 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "build": "gulp", "lint": "esw *.js server config --color", "lint:watch": "yarn lint -- --watch", + "precommit": "yarn lint && yarn test", "test": "cross-env NODE_ENV=test ./node_modules/.bin/mocha --ui bdd --reporter spec --colors --compilers js:babel-core/register ./server/**/*.test.js", "test:watch": "yarn test -- --watch", "test:coverage": "cross-env NODE_ENV=test ./node_modules/.bin/istanbul cover _mocha -- --ui bdd --reporter spec --colors --compilers js:babel-core/register ./server/**/*.test.js", @@ -75,7 +76,6 @@ "eslint-config-airbnb-base": "7.1.0", "eslint-plugin-import": "1.16.0", "eslint-watch": "2.1.14", - "ghooks": "^1.2.4", "gulp": "3.9.1", "gulp-babel": "6.1.2", "gulp-load-plugins": "^1.2.0", @@ -83,6 +83,7 @@ "gulp-nodemon": "^2.0.6", "gulp-sourcemaps": "^1.6.0", "gulp-util": "^3.0.7", + "husky": "^0.13.1", "istanbul": "1.1.0-alpha.1", "mocha": "3.2.0", "run-sequence": "^1.1.5", @@ -92,9 +93,6 @@ }, "license": "MIT", "config": { - "ghooks": { - "pre-commit": "yarn lint && yarn test" - }, "commitizen": { "path": "./node_modules/cz-conventional-changelog" } diff --git a/yarn.lock b/yarn.lock index 09ae8177..f79019a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -978,6 +978,10 @@ chokidar@^1.0.0, chokidar@^1.4.3: optionalDependencies: fsevents "^1.0.0" +ci-info@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.0.0.tgz#dc5285f2b4e251821683681c381c3388f46ec534" + circular-json@^0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.3.1.tgz#be8b36aefccde8b3ca7aa2d6afc07a37242c0d2d" @@ -1198,13 +1202,6 @@ cross-env@3.1.3: dependencies: cross-spawn "^3.0.1" -cross-spawn-async@^2.1.1: - version "2.2.5" - resolved "https://registry.yarnpkg.com/cross-spawn-async/-/cross-spawn-async-2.2.5.tgz#845ff0c0834a3ded9d160daca6d390906bb288cc" - dependencies: - lru-cache "^4.0.0" - which "^1.2.8" - cross-spawn@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-3.0.1.tgz#1256037ecb9f0c5f79e3d6ef135e30770184b982" @@ -1662,17 +1659,6 @@ event-stream@^3.2.1, event-stream@~3.3.0: stream-combiner "~0.0.4" through "~2.3.1" -execa@^0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/execa/-/execa-0.4.0.tgz#4eb6467a36a095fabb2970ff9d5e3fb7bce6ebc3" - dependencies: - cross-spawn-async "^2.1.1" - is-stream "^1.1.0" - npm-run-path "^1.0.0" - object-assign "^4.0.1" - path-key "^1.0.0" - strip-eof "^1.0.0" - exit-hook@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/exit-hook/-/exit-hook-1.1.1.tgz#f05ca233b48c05d54fff07765df8507e95c02ff8" @@ -1854,6 +1840,10 @@ find-node-modules@1.0.4: findup-sync "0.4.2" merge "^1.2.0" +find-parent-dir@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54" + find-root@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/find-root/-/find-root-1.0.0.tgz#962ff211aab25c6520feeeb8d6287f8f6e95807a" @@ -2061,18 +2051,6 @@ getpass@^0.1.1: dependencies: assert-plus "^1.0.0" -ghooks@^1.2.4: - version "1.3.2" - resolved "https://registry.yarnpkg.com/ghooks/-/ghooks-1.3.2.tgz#bee29deec4283e23eb1ff37d94a8120acd4332e9" - dependencies: - execa "^0.4.0" - findup "0.1.5" - lodash.clone "4.3.2" - manage-path "2.0.0" - opt-cli "1.5.1" - path-exists "^2.0.0" - spawn-command "0.0.2" - glob-base@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4" @@ -2486,6 +2464,15 @@ http-status@^0.2.0: version "0.2.5" resolved "https://registry.yarnpkg.com/http-status/-/http-status-0.2.5.tgz#976f91077ea7bfc15277cbcf8c80c4d5c51b49b0" +husky@^0.13.1: + version "0.13.1" + resolved "https://registry.yarnpkg.com/husky/-/husky-0.13.1.tgz#11efc6fc10e0ec4e789776f6582be37d71ba4ccf" + dependencies: + chalk "^1.1.3" + find-parent-dir "^0.3.0" + is-ci "^1.0.9" + normalize-path "^1.0.0" + iconv-lite@0.4.13: version "0.4.13" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2" @@ -2597,6 +2584,12 @@ is-buffer@^1.0.2: version "1.1.4" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.4.tgz#cfc86ccd5dc5a52fa80489111c6920c457e2d98b" +is-ci@^1.0.9: + version "1.0.10" + resolved "https://registry.yarnpkg.com/is-ci/-/is-ci-1.0.10.tgz#f739336b2632365061a9d48270cd56ae3369318e" + dependencies: + ci-info "^1.0.0" + is-dotfile@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.2.tgz#2c132383f39199f8edc268ca01b9b007d205cc4d" @@ -2704,7 +2697,7 @@ is-resolvable@^1.0.0: dependencies: tryit "^1.0.1" -is-stream@^1.0.0, is-stream@^1.1.0: +is-stream@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" @@ -3041,10 +3034,6 @@ lodash._baseassign@^3.0.0: lodash._basecopy "^3.0.0" lodash.keys "^3.0.0" -lodash._baseclone@~4.5.0: - version "4.5.7" - resolved "https://registry.yarnpkg.com/lodash._baseclone/-/lodash._baseclone-4.5.7.tgz#ce42ade08384ef5d62fa77c30f61a46e686f8434" - lodash._basecopy@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz#8da0e6a876cf344c0ad8a54882111dd3c5c7ca36" @@ -3109,12 +3098,6 @@ lodash.assignwith@^4.0.7: version "4.2.0" resolved "https://registry.yarnpkg.com/lodash.assignwith/-/lodash.assignwith-4.2.0.tgz#127a97f02adc41751a954d24b0de17e100e038eb" -lodash.clone@4.3.2: - version "4.3.2" - resolved "https://registry.yarnpkg.com/lodash.clone/-/lodash.clone-4.3.2.tgz#e56b176b6823a7dde38f7f2bf58de7d5971200e9" - dependencies: - lodash._baseclone "~4.5.0" - lodash.cond@^4.3.0: version "4.5.2" resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5" @@ -3267,17 +3250,13 @@ lru-cache@2: version "2.7.3" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952" -lru-cache@^4.0.0, lru-cache@^4.0.1: +lru-cache@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e" dependencies: pseudomap "^1.0.1" yallist "^2.0.0" -manage-path@2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/manage-path/-/manage-path-2.0.0.tgz#f4cf8457b926eeee2a83b173501414bc76eb9597" - map-cache@^0.2.0: version "0.2.2" resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" @@ -3552,16 +3531,14 @@ nopt@~1.0.10: dependencies: abbrev "1" +normalize-path@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-1.0.0.tgz#32d0e472f91ff345701c15a8311018d3b0a90379" + normalize-path@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.0.1.tgz#47886ac1662760d4261b7d979d241709d3ce3f7a" -npm-run-path@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-1.0.0.tgz#f5c32bf595fe81ae927daec52e82f8b000ac3c8f" - dependencies: - path-key "^1.0.0" - npmlog@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.0.2.tgz#d03950e0e78ce1527ba26d2a7592e9348ac3e75f" @@ -3624,15 +3601,6 @@ onetime@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/onetime/-/onetime-1.1.0.tgz#a1f7838f8314c516f05ecefcbc4ccfe04b4ed789" -opt-cli@1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/opt-cli/-/opt-cli-1.5.1.tgz#04db447b13c96b992eb31685266f4ed0d9736dc2" - dependencies: - commander "2.9.0" - lodash.clone "4.3.2" - manage-path "2.0.0" - spawn-command "0.0.2-1" - optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" @@ -3742,10 +3710,6 @@ path-is-inside@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53" -path-key@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/path-key/-/path-key-1.0.0.tgz#5d53d578019646c0d68800db4e146e6bdc2ac7af" - path-parse@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" @@ -4335,14 +4299,6 @@ sparkles@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/sparkles/-/sparkles-1.0.0.tgz#1acbbfb592436d10bbe8f785b7cc6f82815012c3" -spawn-command@0.0.2: - version "0.0.2" - resolved "https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2.tgz#9544e1a43ca045f8531aac1a48cb29bdae62338e" - -spawn-command@0.0.2-1: - version "0.0.2-1" - resolved "https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2-1.tgz#62f5e9466981c1b796dc5929937e11c9c6921bd0" - spawn-sync@^1.0.15: version "1.0.15" resolved "https://registry.yarnpkg.com/spawn-sync/-/spawn-sync-1.0.15.tgz#b00799557eb7fb0c8376c29d44e8a1ea67e57476" @@ -4453,10 +4409,6 @@ strip-bom@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3" -strip-eof@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" - strip-json-comments@2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" @@ -4788,7 +4740,7 @@ vinyl@^0.5.0: clone-stats "^0.0.1" replace-ext "0.0.1" -which@^1.1.1, which@^1.2.12, which@^1.2.8, which@^1.2.9: +which@^1.1.1, which@^1.2.12, which@^1.2.9: version "1.2.12" resolved "https://registry.yarnpkg.com/which/-/which-1.2.12.tgz#de67b5e450269f194909ef23ece4ebe416fa1192" dependencies: From 2f438302dfba9393b4c41cc7529215b91076c271 Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Sat, 18 Feb 2017 20:40:13 +0530 Subject: [PATCH 2/6] Develop license and support (#298) Add Support and License in README --- LICENSE | 2 +- README.md | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 35ac03cf..d5f9abab 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Kunal Kapadia +Copyright (c) 2016-2017 Kunal Kapadia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 70cb1926..e490d0ff 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)](http://commitizen.github.io/cz-cli/) [![MIT License](https://img.shields.io/npm/l/stack-overflow-copy-paste.svg?style=flat-square)](http://opensource.org/licenses/MIT) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) +[![Support via Paypal](https://img.shields.io/badge/support-paypal-yellowgreen.svg?style=flat-square)](https://www.paypal.me/KunalKapadia) # [![Express ES6 REST API Starter](https://cloud.githubusercontent.com/assets/4172932/12660610/90f5b856-c63a-11e5-878e-c9f0bbf33090.jpg)](https://github.com/KunalKapadia/express-mongoose-es6-rest-api) @@ -149,6 +150,14 @@ If you would prefer not to use any of our tooling, delete the following files fr Contributions, questions and comments are all welcome and encouraged. For code contributions submit a pull request with unit test. +## License +This project is licensed under the [MIT License](https://github.com/KunalKapadia/express-mongoose-es6-rest-api/blob/master/LICENSE) + +## Support Development +If this project saved your valuable time in getting your service up, and you feel like buying me coffee, you can donate either at my BTC address: `1LkW5UoERR1jjJsChMheKuo6vn95x2mzWg` or at [![Support via Paypal](https://img.shields.io/badge/support-paypal-yellowgreen.svg?style=flat-square)](https://www.paypal.me/KunalKapadia) + +Your support is greatly appreciated. + ## Meta Kunal Kapadia – [@kunalkapadia12](https://twitter.com/KunalKapadia12) – kunalkapadia12@gmail.com From a4d2709033e7394fa2bd9a800625683f8d2ecc57 Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Sat, 18 Feb 2017 20:52:32 +0530 Subject: [PATCH 3/6] Add JWT test (#299) --- server/tests/auth.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server/tests/auth.test.js b/server/tests/auth.test.js index 5e87859a..9b781d99 100644 --- a/server/tests/auth.test.js +++ b/server/tests/auth.test.js @@ -63,6 +63,18 @@ describe('## Auth APIs', () => { .catch(done); }); + it('should fail to get random number because of wrong token', (done) => { + request(app) + .get('/api/auth/random-number') + .set('Authorization', 'Bearer inValidToken') + .expect(httpStatus.UNAUTHORIZED) + .then((res) => { + expect(res.body.message).to.equal('Unauthorized'); + done(); + }) + .catch(done); + }); + it('should get a random number', (done) => { request(app) .get('/api/auth/random-number') From 85777d48d1d92aa0bc4d67662c4b9a05d175f552 Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Sat, 18 Feb 2017 21:04:25 +0530 Subject: [PATCH 4/6] Use del.sync instead of del (#301) --- gulpfile.babel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 13acbd6f..2e40ddb4 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -14,7 +14,7 @@ const paths = { // Clean up dist and coverage directory gulp.task('clean', () => - del(['dist/**', 'coverage/**', '!dist', '!coverage']) + del.sync(['dist/**', 'coverage/**', '!dist', '!coverage']) ); // Copy non-js files to dist From 3e520a86c0d12c78d69e681fb64f1e598b45e45d Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Sun, 19 Feb 2017 21:40:02 +0530 Subject: [PATCH 5/6] Use --recursive to run nested test files (#304) Closes #303 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1d568771..ec94fa02 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,9 @@ "lint": "esw *.js server config --color", "lint:watch": "yarn lint -- --watch", "precommit": "yarn lint && yarn test", - "test": "cross-env NODE_ENV=test ./node_modules/.bin/mocha --ui bdd --reporter spec --colors --compilers js:babel-core/register ./server/**/*.test.js", + "test": "cross-env NODE_ENV=test ./node_modules/.bin/mocha --ui bdd --reporter spec --colors --compilers js:babel-core/register server/tests --recursive", "test:watch": "yarn test -- --watch", - "test:coverage": "cross-env NODE_ENV=test ./node_modules/.bin/istanbul cover _mocha -- --ui bdd --reporter spec --colors --compilers js:babel-core/register ./server/**/*.test.js", + "test:coverage": "cross-env NODE_ENV=test ./node_modules/.bin/istanbul cover _mocha -- --ui bdd --reporter spec --colors --compilers js:babel-core/register server/tests --recursive", "test:check-coverage": "yarn test:coverage && istanbul check-coverage", "report-coverage": "coveralls < ./coverage/lcov.info" }, From c1e00549ea647aa78bbd48c3483d239ecc40af26 Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Mon, 20 Feb 2017 20:26:05 +0530 Subject: [PATCH 6/6] Use dotenv for configuration management (#302) --- .env.example | 5 +++ .gitignore | 3 ++ .istanbul.yml | 4 +-- config/config.js | 44 +++++++++++++++++++++++++++ config/env/development.js | 9 ------ config/env/index.js | 10 ------ config/env/production.js | 8 ----- config/env/test.js | 8 ----- config/express.js | 2 +- gulpfile.babel.js | 4 +-- index.js | 7 +++-- package.json | 3 +- server/controllers/auth.controller.js | 2 +- server/routes/auth.route.js | 2 +- server/tests/auth.test.js | 2 +- yarn.lock | 4 +++ 16 files changed, 71 insertions(+), 46 deletions(-) create mode 100644 .env.example create mode 100644 config/config.js delete mode 100644 config/env/development.js delete mode 100644 config/env/index.js delete mode 100644 config/env/production.js delete mode 100644 config/env/test.js diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..02bb8abf --- /dev/null +++ b/.env.example @@ -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 diff --git a/.gitignore b/.gitignore index f1956130..d70e27f4 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,6 @@ node_modules # Optional REPL history .node_repl_history + +# .env +.env diff --git a/.istanbul.yml b/.istanbul.yml index deb39607..b67186b0 100644 --- a/.istanbul.yml +++ b/.istanbul.yml @@ -1,6 +1,6 @@ verbose: false instrumentation: - excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js', 'config/env/development.js', 'config/env/production.js'] + excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js'] include-all-sources: true reporting: print: summary @@ -22,4 +22,4 @@ check: statements: 50 lines: 50 branches: 30 - functions: 20 \ No newline at end of file + functions: 20 diff --git a/config/config.js b/config/config.js new file mode 100644 index 00000000..3ca2c9ff --- /dev/null +++ b/config/config.js @@ -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; diff --git a/config/env/development.js b/config/env/development.js deleted file mode 100644 index f9f95e98..00000000 --- a/config/env/development.js +++ /dev/null @@ -1,9 +0,0 @@ -const devConfig = { - env: 'development', - MONGOOSE_DEBUG: true, - jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', - db: 'mongodb://localhost/express-mongoose-es6-rest-api-development', - port: 4040 -}; - -export default devConfig; diff --git a/config/env/index.js b/config/env/index.js deleted file mode 100644 index 6d9e0812..00000000 --- a/config/env/index.js +++ /dev/null @@ -1,10 +0,0 @@ -import path from 'path'; - -const env = process.env.NODE_ENV || 'development'; -const config = require(`./${env}`); // eslint-disable-line import/no-dynamic-require - -const defaults = { - root: path.join(__dirname, '/..') -}; - -export default Object.assign(defaults, config); diff --git a/config/env/production.js b/config/env/production.js deleted file mode 100644 index 879cad6b..00000000 --- a/config/env/production.js +++ /dev/null @@ -1,8 +0,0 @@ -const prodConfig = { - env: 'production', - jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', - db: 'mongodb://localhost/express-mongoose-es6-rest-api-production', - port: 4040 -}; - -export default prodConfig; diff --git a/config/env/test.js b/config/env/test.js deleted file mode 100644 index f168c4e9..00000000 --- a/config/env/test.js +++ /dev/null @@ -1,8 +0,0 @@ -const testConfig = { - env: 'test', - jwtSecret: '0a6b944d-d2fb-46fc-a85e-0295c986cd9f', - db: 'mongodb://localhost/express-mongoose-es6-rest-api-test', - port: 4040 -}; - -export default testConfig; diff --git a/config/express.js b/config/express.js index 3d058ada..555d3a63 100644 --- a/config/express.js +++ b/config/express.js @@ -11,7 +11,7 @@ import expressValidation from 'express-validation'; import helmet from 'helmet'; import winstonInstance from './winston'; import routes from '../server/routes/index.route'; -import config from './env'; +import config from './config'; import APIError from '../server/helpers/APIError'; const app = express(); diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 2e40ddb4..53155cb3 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -8,13 +8,13 @@ const plugins = gulpLoadPlugins(); const paths = { js: ['./**/*.js', '!dist/**', '!node_modules/**', '!coverage/**'], - nonJs: ['./package.json', './.gitignore'], + nonJs: ['./package.json', './.gitignore', './.env'], tests: './server/tests/*.js' }; // Clean up dist and coverage directory gulp.task('clean', () => - del.sync(['dist/**', 'coverage/**', '!dist', '!coverage']) + del.sync(['dist/**', 'dist/.*', 'coverage/**', '!dist', '!coverage']) ); // Copy non-js files to dist diff --git a/index.js b/index.js index f56c7d42..7bd9ba17 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,8 @@ import mongoose from 'mongoose'; import util from 'util'; -import config from './config/env'; + +// config should be imported before importing any other file +import config from './config/config'; import app from './config/express'; const debug = require('debug')('express-mongoose-es6-rest-api:index'); @@ -12,7 +14,8 @@ Promise = require('bluebird'); // eslint-disable-line no-global-assign mongoose.Promise = Promise; // connect to mongo db -mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } }); +const mongoUri = `${config.mongo.host}:${config.mongo.port}`; +mongoose.connect(mongoUri, { server: { socketOptions: { keepAlive: 1 } } }); mongoose.connection.on('error', () => { throw new Error(`unable to connect to database: ${config.db}`); }); diff --git a/package.json b/package.json index ec94fa02..1444a8d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-mongoose-es6-rest-api", - "version": "1.0.0", + "version": "2.0.0", "description": "A Boilerplate application for building REST APIs using express, mongoose in ES6 with code coverage", "author": "Kunal Kapadia ", "main": "index.js", @@ -47,6 +47,7 @@ "cookie-parser": "1.4.3", "cors": "2.8.1", "debug": "^2.4.5", + "dotenv": "^4.0.0", "express": "4.14.0", "express-jwt": "5.1.0", "express-validation": "1.0.1", diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index 2ea47632..f8403e76 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -1,7 +1,7 @@ import jwt from 'jsonwebtoken'; import httpStatus from 'http-status'; import APIError from '../helpers/APIError'; -import config from '../../config/env'; +import config from '../../config/config'; // sample user, used for authentication const user = { diff --git a/server/routes/auth.route.js b/server/routes/auth.route.js index cdf37b63..6ccf23a2 100644 --- a/server/routes/auth.route.js +++ b/server/routes/auth.route.js @@ -3,7 +3,7 @@ import validate from 'express-validation'; import expressJwt from 'express-jwt'; import paramValidation from '../../config/param-validation'; import authCtrl from '../controllers/auth.controller'; -import config from '../../config/env'; +import config from '../../config/config'; const router = express.Router(); // eslint-disable-line new-cap diff --git a/server/tests/auth.test.js b/server/tests/auth.test.js index 9b781d99..f2711021 100644 --- a/server/tests/auth.test.js +++ b/server/tests/auth.test.js @@ -3,7 +3,7 @@ import httpStatus from 'http-status'; import jwt from 'jsonwebtoken'; import chai, { expect } from 'chai'; import app from '../../index'; -import config from '../../config/env'; +import config from '../../config/config'; chai.config.includeStack = true; diff --git a/yarn.lock b/yarn.lock index f79019a0..9c67e441 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1397,6 +1397,10 @@ dont-sniff-mimetype@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/dont-sniff-mimetype/-/dont-sniff-mimetype-1.0.0.tgz#5932890dc9f4e2f19e5eb02a20026e5e5efc8f58" +dotenv@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-4.0.0.tgz#864ef1379aced55ce6f95debecdce179f7a0cd1d" + duplexer2@0.0.2: version "0.0.2" resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.0.2.tgz#c614dcf67e2fb14995a91711e5a617e8a60a31db"