Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 06ea57c

Browse files
author
Bowden Kelly
authored
Merge pull request #63 from Microsoft/connectionRework
made some changes to mongo connection
2 parents b8b9638 + ffd44de commit 06ea57c

File tree

4 files changed

+60
-80
lines changed

4 files changed

+60
-80
lines changed

.env.example

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,5 @@ MONGOLAB_URI=mongodb://localhost:27017
33

44
SESSION_SECRET=***REMOVED***
55

6-
MAILGUN_USER=***REMOVED***
7-
MAILGUN_PASSWORD=***REMOVED***
8-
9-
SENDGRID_USER=***REMOVED***
10-
SENDGRID_PASSWORD=***REMOVED***
11-
126
FACEBOOK_ID=***REMOVED***
13-
FACEBOOK_SECRET=***REMOVED***
14-
15-
INSTAGRAM_ID=***REMOVED***
16-
INSTAGRAM_SECRET=***REMOVED***
17-
18-
GITHUB_ID=***REMOVED***
19-
GITHUB_SECRET=***REMOVED***
20-
21-
TWITTER_KEY=***REMOVED***
22-
TWITTER_SECRET=***REMOVED***
23-
24-
GOOGLE_ID=***REMOVED***
25-
GOOGLE_SECRET=***REMOVED***
26-
27-
TWILIO_SID=***REMOVED***
28-
TWILIO_TOKEN=***REMOVED***
29-
7+
FACEBOOK_SECRET=***REMOVED***

package-lock.json

Lines changed: 29 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@
99
"author": "Bowden Kelly",
1010
"license": "MIT",
1111
"scripts": {
12-
"start": "npm run build && npm run watch",
12+
"start": "npm run serve",
1313
"build": "npm run build-sass && npm run build-ts && npm run tslint && npm run copy-static-assets",
14-
"serve": "nodemon dist/server.js",
15-
"watch": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve\"",
14+
"serve": "node dist/server.js",
15+
"watch-node": "nodemon dist/server.js",
16+
"watch": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run watch-node\"",
1617
"test": "jest --forceExit",
1718
"build-ts": "tsc",
1819
"watch-ts": "tsc -w",
1920
"build-sass": "node-sass src/public/css/main.scss dist/public/css/main.css",
2021
"watch-sass": "node-sass -w src/public/css/main.scss dist/public/css/main.css",
2122
"tslint": "tslint -c tslint.json -p tsconfig.json",
22-
"copy-static-assets": "node copyStaticAssets.js",
23-
"debug": "npm run build && npm run watch-debug",
24-
"serve-debug": "nodemon --inspect dist/server.js",
25-
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\""
23+
"copy-static-assets": "node copyStaticAssets.js"
2624
},
2725
"jest": {
2826
"globals": {
@@ -45,6 +43,7 @@
4543
"dependencies": {
4644
"async": "^2.5.0",
4745
"bcrypt-nodejs": "^0.0.3",
46+
"bluebird": "^3.5.1",
4847
"body-parser": "^1.18.2",
4948
"compression": "^1.7.1",
5049
"connect-mongo": "^1.3.2",
@@ -57,7 +56,7 @@
5756
"fbgraph": "^1.4.1",
5857
"lodash": "^4.17.4",
5958
"lusca": "^1.5.2",
60-
"mongoose": "^4.12.4",
59+
"mongoose": "^4.13.7",
6160
"morgan": "^1.9.0",
6261
"nodemailer": "^2.7.2",
6362
"passport": "^0.4.0",
@@ -69,6 +68,7 @@
6968
"devDependencies": {
7069
"@types/async": "^2.0.40",
7170
"@types/bcrypt-nodejs": "0.0.30",
71+
"@types/bluebird": "^3.5.18",
7272
"@types/body-parser": "^1.16.2",
7373
"@types/compression": "0.0.33",
7474
"@types/connect-mongo": "0.0.34",

src/app.ts

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Module dependencies.
3-
*/
41
import * as express from "express";
52
import * as compression from "compression"; // compresses requests
63
import * as session from "express-session";
@@ -13,50 +10,38 @@ import * as flash from "express-flash";
1310
import * as path from "path";
1411
import * as mongoose from "mongoose";
1512
import * as passport from "passport";
16-
import expressValidator = require("express-validator");
13+
import * as expressValidator from "express-validator";
14+
import * as bluebird from "bluebird";
1715

1816
const MongoStore = mongo(session);
1917

20-
/**
21-
* Load environment variables from .env file, where API keys and passwords are configured.
22-
*/
18+
// Load environment variables from .env file, where API keys and passwords are configured
2319
dotenv.config({ path: ".env.example" });
2420

25-
26-
/**
27-
* Controllers (route handlers).
28-
*/
21+
// Controllers (route handlers)
2922
import * as homeController from "./controllers/home";
3023
import * as userController from "./controllers/user";
3124
import * as apiController from "./controllers/api";
3225
import * as contactController from "./controllers/contact";
3326

34-
/**
35-
* API keys and Passport configuration.
36-
*/
27+
28+
// API keys and Passport configuration
3729
import * as passportConfig from "./config/passport";
3830

39-
/**
40-
* Create Express server.
41-
*/
31+
// Create Express server
4232
const app = express();
4333

44-
/**
45-
* Connect to MongoDB.
46-
*/
47-
// mongoose.Promise = global.Promise;
48-
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
49-
50-
mongoose.connection.on("error", () => {
51-
console.log("MongoDB connection error. Please make sure MongoDB is running.");
52-
process.exit();
34+
// Connect to MongoDB
35+
const mongoUrl = process.env.MONGOLAB_URI;
36+
(<any>mongoose).Promise = bluebird;
37+
mongoose.connect(mongoUrl, {useMongoClient: true}).then(
38+
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
39+
).catch(err => {
40+
console.log("MongoDB connection error. Please make sure MongoDB is running. " + err);
41+
// process.exit();
5342
});
5443

55-
56-
57-
/**
58-
* Express configuration.
59-
*/
44+
// Express configuration
6045
app.set("port", process.env.PORT || 3000);
6146
app.set("views", path.join(__dirname, "../views"));
6247
app.set("view engine", "pug");
@@ -70,7 +55,7 @@ app.use(session({
7055
saveUninitialized: true,
7156
secret: process.env.SESSION_SECRET,
7257
store: new MongoStore({
73-
url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
58+
url: mongoUrl,
7459
autoReconnect: true
7560
})
7661
}));
@@ -86,13 +71,13 @@ app.use((req, res, next) => {
8671
app.use((req, res, next) => {
8772
// After successful login, redirect back to the intended page
8873
if (!req.user &&
89-
req.path !== "/login" &&
90-
req.path !== "/signup" &&
91-
!req.path.match(/^\/auth/) &&
92-
!req.path.match(/\./)) {
74+
req.path !== "/login" &&
75+
req.path !== "/signup" &&
76+
!req.path.match(/^\/auth/) &&
77+
!req.path.match(/\./)) {
9378
req.session.returnTo = req.path;
9479
} else if (req.user &&
95-
req.path == "/account") {
80+
req.path == "/account") {
9681
req.session.returnTo = req.path;
9782
}
9883
next();

0 commit comments

Comments
 (0)