From 5da7c3a251da2b65bf4448ac5ccbc5715b09efaf Mon Sep 17 00:00:00 2001 From: Sparsh Temani Date: Thu, 23 Jul 2020 00:55:45 +0530 Subject: [PATCH 1/2] managing global configuration --- .gitignore | 2 +- README.md | 2 +- config/example.json | 12 ++++++++++++ package-lock.json | 25 +++++++++++++++++++++++-- package.json | 2 ++ src/app.ts | 12 ++++++++---- src/config.example.ts | 5 ----- src/middleware/auth.middleware.ts | 6 ++++-- src/utils/auth.util.ts | 6 ++++-- 9 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 config/example.json delete mode 100644 src/config.example.ts diff --git a/.gitignore b/.gitignore index ec5f103..c9737c2 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ node_modules/ jspm_packages/ # Configuration file -src/config.ts +config/default.json # TypeScript v1 declaration files typings/ diff --git a/README.md b/README.md index 5835f92..e3d98ee 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - Install dependencies: ``` npm install ``` -**Note:** Make sure to add a `config.js` file in the `src` folder. See the `config.example.js` under `src/` directory for more details. +**Note:** Make sure to add a `default.json` file in the `config` folder. Refer to `config/example.json` for an example. For production, create a `production.json` and follow the same format. diff --git a/config/example.json b/config/example.json new file mode 100644 index 0000000..519cd81 --- /dev/null +++ b/config/example.json @@ -0,0 +1,12 @@ +{ + "app": { + "PORT": 4000, + "auth": { + "secretKey": "", + "tokenExpiry": 1 + } + }, + "db": { + "mongoUrl": "mongodb://127.0.0.1:27017/posterior" + } +} diff --git a/package-lock.json b/package-lock.json index bbc58dc..8e569d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -82,6 +82,12 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, + "@types/config": { + "version": "0.0.36", + "resolved": "https://registry.npmjs.org/@types/config/-/config-0.0.36.tgz", + "integrity": "sha512-EoAeT1MyFWh2BJvBDEFInY714bQBbHOAucqxqqhprhbBFqr+B7fuN5T9CJqUIGDzvwubnKKRqmSo6yPo0aSpNw==", + "dev": true + }, "@types/connect": { "version": "3.4.33", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.33.tgz", @@ -746,6 +752,14 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, + "config": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/config/-/config-3.3.1.tgz", + "integrity": "sha512-+2/KaaaAzdwUBE3jgZON11L1ggLLhpf2FsGrfqYFHZW22ySGv/HqYIXrBwKKvn+XZh1UBUjHwAcrfsSkSygT+Q==", + "requires": { + "json5": "^2.1.1" + } + }, "configstore": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/configstore/-/configstore-5.0.1.tgz", @@ -1720,6 +1734,14 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, + "json5": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.3.tgz", + "integrity": "sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==", + "requires": { + "minimist": "^1.2.5" + } + }, "jsonwebtoken": { "version": "8.5.1", "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", @@ -1938,8 +1960,7 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "mkdirp": { "version": "0.5.5", diff --git a/package.json b/package.json index 6e47b37..49e0fb6 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ }, "homepage": "https://github.com/pes-alcoding-club/posterior#readme", "dependencies": { + "config": "^3.3.1", "express": "^4.17.1", "jsonwebtoken": "^8.5.1", "mongoose": "^5.9.22", @@ -41,6 +42,7 @@ "passport-local-mongoose": "^6.0.1" }, "devDependencies": { + "@types/config": "0.0.36", "@types/express": "^4.17.7", "@types/mongoose": "^5.7.31", "@types/node": "^14.0.22", diff --git a/src/app.ts b/src/app.ts index 1155785..c3b41c5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ import mongoose from 'mongoose'; import passport from 'passport'; -import config from './config'; +import config from 'config'; import AuthRouter from './routers/auth.router'; @@ -12,18 +12,22 @@ app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(passport.initialize()); -app.listen(config.PORT, (err: any) => { +const appConfig : any = config.get('app'); + +app.listen(appConfig.PORT, (err: any) => { if (err) { console.error(`Unable to start app. Found error: ${err.message}`); return; } - console.error(`Server Running at PORT: ${config.PORT}`); + console.error(`Server Running at PORT: ${appConfig.PORT}`); }); app.use('/api/auth', AuthRouter); +const dbConfig : any = config.get('db'); + mongoose.set('useCreateIndex', true); -mongoose.connect(config.mongoUrl, { +mongoose.connect(dbConfig.mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }); diff --git a/src/config.example.ts b/src/config.example.ts deleted file mode 100644 index 7a020dc..0000000 --- a/src/config.example.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default { - secretKey: '', - mongoUrl: '', - PORT: 4000 -}; \ No newline at end of file diff --git a/src/middleware/auth.middleware.ts b/src/middleware/auth.middleware.ts index 8be3da0..db9e244 100644 --- a/src/middleware/auth.middleware.ts +++ b/src/middleware/auth.middleware.ts @@ -8,7 +8,7 @@ const ExtractJwt = passportJwt.ExtractJwt; import User from '../models/user.model'; -import config from '../config'; +import config from 'config'; import { NextFunction } from 'express'; @@ -16,9 +16,11 @@ export const local = passport.use(new LocalStrategy(User.authenticate())); passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser()); +const authConfig : any = config.get('app.auth'); + const opts = { jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), - secretOrKey: config.secretKey + secretOrKey: authConfig.secretKey }; export const jwtPasport = passport.use( diff --git a/src/utils/auth.util.ts b/src/utils/auth.util.ts index 28eae96..31424e3 100644 --- a/src/utils/auth.util.ts +++ b/src/utils/auth.util.ts @@ -1,6 +1,8 @@ import jwt from 'jsonwebtoken'; -import config from '../config'; +import config from 'config'; + +const authConfig : any = config.get('app.auth'); export const getToken = function (user: any) { - return jwt.sign(user, config.secretKey, { expiresIn: 30 * 24 * 3600 }); // Expires in 30 days + return jwt.sign(user, authConfig.secretKey, { expiresIn: authConfig.tokenExpiry * 24 * 3600 }); }; From 01c3ce56157056055498e4291091f49b83c93d9e Mon Sep 17 00:00:00 2001 From: Sparsh Temani Date: Fri, 24 Jul 2020 10:33:54 +0530 Subject: [PATCH 2/2] Minor changes --- .gitignore | 2 +- README.md | 2 +- config/default.json | 12 ++++++++++++ src/app.ts | 4 ++-- src/middleware/auth.middleware.ts | 2 +- src/utils/auth.util.ts | 6 ++++-- 6 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 config/default.json diff --git a/.gitignore b/.gitignore index c9737c2..4ff3a8f 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ node_modules/ jspm_packages/ # Configuration file -config/default.json +config/local.json # TypeScript v1 declaration files typings/ diff --git a/README.md b/README.md index e3d98ee..c9b98fa 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - Install dependencies: ``` npm install ``` -**Note:** Make sure to add a `default.json` file in the `config` folder. Refer to `config/example.json` for an example. For production, create a `production.json` and follow the same format. +**Note:** To Overide changes in `default.json`, create a file named `local.json` and add in the new changes here. For more info, refer [here.](https://github.com/lorenwest/node-config/wiki/Configuration-Files#config-directory) diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..2bffc0b --- /dev/null +++ b/config/default.json @@ -0,0 +1,12 @@ +{ + "app": { + "PORT": 4000, + "auth": { + "secretKey": "", + "tokenExpiry": 1 + } + }, + "db": { + "mongoUrl": "mongodb://127.0.0.1:27017/posterior" + } +} \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index c3b41c5..cf48cfa 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,7 +12,7 @@ app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(passport.initialize()); -const appConfig : any = config.get('app'); +const appConfig: any = config.get('app'); app.listen(appConfig.PORT, (err: any) => { if (err) { @@ -24,7 +24,7 @@ app.listen(appConfig.PORT, (err: any) => { app.use('/api/auth', AuthRouter); -const dbConfig : any = config.get('db'); +const dbConfig: any = config.get('db'); mongoose.set('useCreateIndex', true); mongoose.connect(dbConfig.mongoUrl, { diff --git a/src/middleware/auth.middleware.ts b/src/middleware/auth.middleware.ts index db9e244..9c15ff1 100644 --- a/src/middleware/auth.middleware.ts +++ b/src/middleware/auth.middleware.ts @@ -16,7 +16,7 @@ export const local = passport.use(new LocalStrategy(User.authenticate())); passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser()); -const authConfig : any = config.get('app.auth'); +const authConfig: any = config.get('app.auth'); const opts = { jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), diff --git a/src/utils/auth.util.ts b/src/utils/auth.util.ts index 31424e3..9dec1da 100644 --- a/src/utils/auth.util.ts +++ b/src/utils/auth.util.ts @@ -1,8 +1,10 @@ import jwt from 'jsonwebtoken'; import config from 'config'; -const authConfig : any = config.get('app.auth'); +const authConfig: any = config.get('app.auth'); export const getToken = function (user: any) { - return jwt.sign(user, authConfig.secretKey, { expiresIn: authConfig.tokenExpiry * 24 * 3600 }); + return jwt.sign(user, authConfig.secretKey, { + expiresIn: authConfig.tokenExpiry * 24 * 3600 + }); };