Skip to content

Commit

Permalink
added env variables logic
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmaguitar committed Nov 21, 2016
1 parent e6fc360 commit be3337b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 12 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser')
const aws = require('aws-sdk');

const config = require('./config');

const getRouterRestaurants = require('./routes/restaurants');
const getRouterRestaurant = require('./routes/restaurant');

const prepareParams = require('./routes/middleware/prepareParams');

const PORT = 3000;
const S3_BUCKET = process.env.S3_BUCKET;
const ENVIRONMENT = process.env.ENVIRONMENT || 'development';
const urlDB = config[ENVIRONMENT].db;

if (ENVIRONMENT === 'production') {
const USER_DB = process.env.USER_DB;
const PASS_DB = process.env.PASS_DB;
urlDB = urlDB.replace("<%USER_DB%>", USER_DB)
urlDB = urlDB.replace("<%PASS_DB%>", PASS_DB)
}

const app = express();

Expand All @@ -24,10 +35,7 @@ app.use( bodyParser.json() )

app.use( prepareParams )

// Connection URL
const url = 'mongodb://localhost:27017/test';

MongoClient.connect(url)
MongoClient.connect( urlDB )
.then( (db) => {

console.log("Connected to DB...")
Expand Down
8 changes: 8 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
'development' : {
'db' : 'mongodb://localhost:27017/test'
},
'production' : {
'db' : 'mongodb://<%USER_DB%>:<%PASS_DB%>@ds159497.mlab.com:59497/restaurants-ny'
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"devtool": "devtool app --watch",
"dev": "nodemon app"
"devtool": "ENVIRONMENT=development devtool app --watch",
"dev": "ENVIRONMENT=development nodemon app"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit be3337b

Please sign in to comment.