From a00a3efbf6a93a21c49c9c3e69576d89e41d9565 Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Wed, 25 Jul 2018 10:18:29 -0400 Subject: [PATCH] Add different config for morgan logging --- config/default.json | 3 ++- config/dev.json | 3 ++- config/test.json | 3 ++- server.js | 27 +++++++++++---------------- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/config/default.json b/config/default.json index d8e9df4..bc08799 100644 --- a/config/default.json +++ b/config/default.json @@ -1,3 +1,4 @@ { - "dbHost": "mongodb://localhost:27017/shimapan" + "dbHost": "mongodb://localhost:27017/shimapan", + "httpLogLevel": "combined" } diff --git a/config/dev.json b/config/dev.json index d8e9df4..84be811 100644 --- a/config/dev.json +++ b/config/dev.json @@ -1,3 +1,4 @@ { - "dbHost": "mongodb://localhost:27017/shimapan" + "dbHost": "mongodb://localhost:27017/shimapan", + "httpLogLevel": "dev" } diff --git a/config/test.json b/config/test.json index e3a350d..376b1b6 100644 --- a/config/test.json +++ b/config/test.json @@ -1,3 +1,4 @@ { - "dbHost": "mongodb://localhost:27017/shimapan-test" + "dbHost": "mongodb://localhost:27017/shimapan-test", + "httpLogLevel": "dev" } \ No newline at end of file diff --git a/server.js b/server.js index 0af7f8f..961ac21 100755 --- a/server.js +++ b/server.js @@ -9,24 +9,18 @@ const sanitizer = require('express-sanitizer'); const helmet = require('helmet'); const app = express(); - const config = require('config'); -if(config.util.getEnv('NODE_ENV') !== 'test') { - app.use(morgan('combined')); -} -mongoose.Promise = global.Promise; +// MongoDB mongoose.connect(config.dbHost, {useNewUrlParser: true}); const db = mongoose.connection; -db.on('error', function(err) { - if (err) console.log('MongoDB Connection Error: ', err); -}); const MongoStore = require('connect-mongo')(session); -const mongoStore = new MongoStore({ - url: config.dbHost -}); +const mongoStore = new MongoStore({url: config.dbHost}); +// HTTP Request Logging +app.use(morgan(config.httpLogLevel)); +// Session setup app.use(helmet()); app.set('trust proxy', 1); app.use(session({ @@ -42,6 +36,8 @@ app.use(session({ maxAge: 1000 * 60 * 60 } })); + +// Middleware app.use(passport.initialize()); app.use(passport.session()); app.use(bodyParser.json()); @@ -51,19 +47,18 @@ app.use(bodyParser.text()); app.use(sanitizer()); app.use(methodOverride('X-HTTP-Method-Override')); - +// Static directories and favicon //app.use(favicon(__dirname + '/public/img/favicon.ico')); app.use(express.static(__dirname + '/public')); - +// Install routes and configure authentication strategy require('./app/routes/routes.js')(app); require('./config/passport.js'); - // Start app const port = process.env.PORT || 8080; -var server = app.listen(port); -console.log('Listening on port ', port, '...'); +const server = app.listen(port); +console.log('Listening on port ' + port + '...\n'); // Handle sigint process.on('SIGINT', () => {