import * as express from 'express' ; import * as bodyParser from 'body-parser' ; import * as cookieParser from 'cookie-parser' ; import * as path from 'path' ; import * as logger from 'morgan' ; import * as mongoose from 'mongoose' ; require ( 'mongoose' ) . Promise = require ( 'bluebird' ) ; const app = express ( ) ; app . use ( logger ( 'dev' ) ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( { extended : false } ) ) ; app . use ( cookieParser ( ) ) ; app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ; const routeModules = require ( 'require-all' ) ( { dirname : __dirname + '/routes' , filter : ( filename ) => { filename = filename . toLowerCase ( ) ; if ( ( filename . endsWith ( '.ts' ) && ! filename . endsWith ( '.spec.ts' ) ) || ( filename . endsWith ( '.js' ) && ! filename . endsWith ( '.spec.js' ) ) ) { return filename . substr ( 0 , filename . length - 3 ) ; } } , map : name => '/' + name } ) ; function resolve ( root , modules ) { for ( const name of Object . keys ( modules ) ) { if ( ! name . startsWith ( '/' ) ) { return ; } const module = modules [ name ] ; if ( module . default && module . default . route ) { console . log ( `Add router ${ root + name }` ) ; const router = module . default as express . Router ; app . use ( root , router ) ; } else { resolve ( root + name , module ) ; } } } resolve ( '' , routeModules ) ; app . use ( ( req , res ) => { res . sendFile ( path . join ( __dirname , 'public/index.html' ) ) ; } ) ; export default app ; mongoose . connect ( 'mongodb://localhost/test' , { useMongoClient : true } ) . then ( ( ) => { console . log ( 'MongoDB connected' ) ; } ) . catch ( ( ) => { console . log ( 'unable to connect to mongoDB' ) ; } ) ;