mirror of
https://github.com/microsoft/TypeScript-Node-Starter.git
synced 2025-11-08 20:27:37 +00:00
made some changes to mongo connection
This commit is contained in:
59
src/app.ts
59
src/app.ts
@@ -1,6 +1,3 @@
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
import * as express from "express";
|
||||
import * as compression from "compression"; // compresses requests
|
||||
import * as session from "express-session";
|
||||
@@ -13,50 +10,38 @@ import * as flash from "express-flash";
|
||||
import * as path from "path";
|
||||
import * as mongoose from "mongoose";
|
||||
import * as passport from "passport";
|
||||
import expressValidator = require("express-validator");
|
||||
import * as expressValidator from "express-validator";
|
||||
import * as bluebird from "bluebird";
|
||||
|
||||
const MongoStore = mongo(session);
|
||||
|
||||
/**
|
||||
* Load environment variables from .env file, where API keys and passwords are configured.
|
||||
*/
|
||||
// Load environment variables from .env file, where API keys and passwords are configured
|
||||
dotenv.config({ path: ".env.example" });
|
||||
|
||||
|
||||
/**
|
||||
* Controllers (route handlers).
|
||||
*/
|
||||
// Controllers (route handlers)
|
||||
import * as homeController from "./controllers/home";
|
||||
import * as userController from "./controllers/user";
|
||||
import * as apiController from "./controllers/api";
|
||||
import * as contactController from "./controllers/contact";
|
||||
|
||||
/**
|
||||
* API keys and Passport configuration.
|
||||
*/
|
||||
|
||||
// API keys and Passport configuration
|
||||
import * as passportConfig from "./config/passport";
|
||||
|
||||
/**
|
||||
* Create Express server.
|
||||
*/
|
||||
// Create Express server
|
||||
const app = express();
|
||||
|
||||
/**
|
||||
* Connect to MongoDB.
|
||||
*/
|
||||
// mongoose.Promise = global.Promise;
|
||||
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
|
||||
|
||||
mongoose.connection.on("error", () => {
|
||||
console.log("MongoDB connection error. Please make sure MongoDB is running.");
|
||||
process.exit();
|
||||
// Connect to MongoDB
|
||||
const mongoUrl = process.env.MONGOLAB_URI;
|
||||
(<any>mongoose).Promise = bluebird;
|
||||
mongoose.connect(mongoUrl, {useMongoClient: true}).then(
|
||||
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
|
||||
).catch(err => {
|
||||
console.log("MongoDB connection error. Please make sure MongoDB is running. " + err);
|
||||
// process.exit();
|
||||
});
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Express configuration.
|
||||
*/
|
||||
// Express configuration
|
||||
app.set("port", process.env.PORT || 3000);
|
||||
app.set("views", path.join(__dirname, "../views"));
|
||||
app.set("view engine", "pug");
|
||||
@@ -70,7 +55,7 @@ app.use(session({
|
||||
saveUninitialized: true,
|
||||
secret: process.env.SESSION_SECRET,
|
||||
store: new MongoStore({
|
||||
url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
|
||||
url: mongoUrl,
|
||||
autoReconnect: true
|
||||
})
|
||||
}));
|
||||
@@ -86,13 +71,13 @@ app.use((req, res, next) => {
|
||||
app.use((req, res, next) => {
|
||||
// After successful login, redirect back to the intended page
|
||||
if (!req.user &&
|
||||
req.path !== "/login" &&
|
||||
req.path !== "/signup" &&
|
||||
!req.path.match(/^\/auth/) &&
|
||||
!req.path.match(/\./)) {
|
||||
req.path !== "/login" &&
|
||||
req.path !== "/signup" &&
|
||||
!req.path.match(/^\/auth/) &&
|
||||
!req.path.match(/\./)) {
|
||||
req.session.returnTo = req.path;
|
||||
} else if (req.user &&
|
||||
req.path == "/account") {
|
||||
req.path == "/account") {
|
||||
req.session.returnTo = req.path;
|
||||
}
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user