import * as bodyParser from "body-parser"; import * as express from "express"; import * as logger from "morgan"; import * as path from "path"; import methodOverride = require("method-override"); import * as mongoose from "mongoose"; import { IndexRoute } from './routes/IndexRoute'; export class Server { public app: express.Application; public static bootstrap(): Server { return new Server(); } constructor() { //create expressjs application this.app = express(); //configure application this.config(); //add routes this.routes(); //add api this.api(); } public api() { //empty for now } public config() { //use json form parser middlware this.app.use(bodyParser.json()); //use query string parser middlware this.app.use(bodyParser.urlencoded({ extended: true })); //use cookie parser middleware //this.app.use(cookieParser("SECRET_GOES_HERE")); //use override middlware this.app.use(methodOverride()); //catch 404 and forward to error handler this.app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { err.status = 404; next(err); }); //error handling //this.app.use(errorHandler()); (mongoose).Promise = global.Promise; const MONGODB_CONNECTION: string = "mongodb://localhost:27017/hor-db"; mongoose.connect(MONGODB_CONNECTION, {useMongoClient: true}).catch((err) => { if(err) process.emit('uncaughtException', new Error(JSON.stringify({syscall: "listen", code: 'DBCONN', msg: err.message}))); }); } public routes() { let router = express.Router(); //IndexRoute IndexRoute.create(router); //use router middleware this.app.use(router); } }