Skip to content

Commit

Permalink
feat: add mongodb and dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Leleux committed Oct 8, 2018
1 parent d605b13 commit 0283053
Show file tree
Hide file tree
Showing 8 changed files with 11,206 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
node_modules
dist
*.log
*.log
.env
10,104 changes: 10,104 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
"clean": "rm -rf dist",
"client": "webpack-dev-server --mode development --config config/webpack.config.js",
"server": "nodemon src/server/bin/www",
"dev": "concurrently \"yarn run server\" \"yarn run client\"",
"dev": "NODE_ENV=development concurrently \"yarn run server\" \"yarn run client\"",
"heroku-postbuild": "yarn run build"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"dotenv": "^6.0.0",
"ejs": "~2.5.7",
"express": "^4.16.3",
"http-errors": "~1.6.2",
"mongodb": "^3.1.6",
"morgan": "~1.9.0",
"node-sass": "^4.9.3",
"node-sass-middleware": "0.11.0",
Expand Down
19 changes: 19 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const sassMiddleware = require('node-sass-middleware');

const assetPath = require('./asset_path.js');

const db = require('./modules/db.js');

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');

Expand All @@ -16,6 +18,23 @@ const serverRoot = path.join(__dirname, '.');

const app = express();

// Connect to DB, and insert default user if necessary
db.connect().then((db) => {
let collection = db.collection('users');
collection.countDocuments().then((res) => {
if (res === 0) {
collection.insertOne({
login: 'laurent',
password: 'laurent',
firstName: 'Laurent',
lastName: 'Leleux'
}).catch((err) => {
console.log('[App] Unable to insert default user');
});
}
})
});

app.locals.assetPath = assetPath;

app.set('views', path.join(__dirname, 'views'));
Expand Down
7 changes: 7 additions & 0 deletions src/server/bin/www
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env node

/**
* Read env variables
*/
if (process.env.NODE_ENV === 'development') {
require('dotenv').config()
}

/**
* Module dependencies.
*/
Expand Down
44 changes: 44 additions & 0 deletions src/server/modules/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Imports
*/
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');



/**
* Variables
*/
// Connection URL
const url = process.env.DB_URL;
// Database Name
const dbName = process.env.DB_DB;



/**
* Connect to the database
*/
let connect = () => {
return new Promise((resolve, reject) => {
const client = new MongoClient(url);
client.connect(function(err) {
if (err) {
console.log("[DB] Unable to connect to server: " + err.message);
reject(err);
} else {
console.log("[DB] Connected successfully to server");
exports.db = client.db(dbName);
resolve(exports.db);
}
});
});
};



/**
* Exports
*/
exports.connect = connect;
exports.db = null; // db will be set after connected;
7 changes: 3 additions & 4 deletions src/server/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var express = require('express');
var router = express.Router();

const db = require('../modules/db.js');

router.get('/current', function(req, res, next) {
res.json({
firstName: 'John',
lastName: 'Doe',
db.db.collection('users').findOne().then((user) => {
res.json(user);
});
});

Expand Down
Loading

0 comments on commit 0283053

Please sign in to comment.