diff --git a/backend/src/app.js b/backend/src/app.js index f54b44f..4564661 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -3,47 +3,66 @@ import express from 'express'; import mongoose from 'mongoose'; import config from 'config'; -/** - * Configures and connects mongoose to the mongoDB instance - */ -const configureDB = () => { - mongoose.connection.on('connected', () => { - console.log(`Mongoose successfully connected to: ${config.db.uri}`); - }); - - mongoose.connection.on('disconnected', () => { - console.log(`Mongoose disconnected from: ${config.db.uri}`); - }); - - mongoose.connection.on('error', err => { - console.log(`Mongoose connection error: ${err}`); - }); - - mongoose.connect(config.db.uri, { - useNewUrlParser: true, - useCreateIndex: true, - // Use native findOneAndUpdate() rather than findAndModify() - useFindAndModify: false, - }); -}; - -const app = express(); - -SwaggerExpress.create(config.swagger, (err, swaggerExpress) => { - if (err) { - throw err; +class App { + constructor() { + this.app = express(); + this.configureDB(); + this.configureSwagger(); } - // install middleware - swaggerExpress.register(app); - // connect db - configureDB(); + /** + * Configures the swagger express middleware + */ + configureSwagger() { + SwaggerExpress.create(config.swagger, (err, swaggerExpress) => { + if (err) { + throw err; + } - app.listen(config.server.port, config.server.host, () => { - console.log( - `API server listening on: http://${config.server.host}:${config.server.port}${config.swagger.basePath}`, - ); - }); -}); + // Register swagger middleware + swaggerExpress.register(this.app); + // Start express app + this.start(); + }); + } + + /** + * Configures and connects mongoose to the mongoDB instance + */ + // eslint-disable-next-line class-methods-use-this + configureDB() { + mongoose.connection.on('connected', () => { + console.log(`Mongoose successfully connected to: ${config.db.uri}`); + }); + + mongoose.connection.on('disconnected', () => { + console.log(`Mongoose disconnected from: ${config.db.uri}`); + }); + + mongoose.connection.on('error', err => { + console.log(`Mongoose connection error: ${err}`); + }); + + mongoose.connect(config.db.uri, { + useNewUrlParser: true, + useCreateIndex: true, + // Use native findOneAndUpdate() rather than findAndModify() + useFindAndModify: false, + }); + } + + /** + * Starts the express app with the settings defined in 'config.server' + */ + start() { + this.app.listen(config.server.port, config.server.host, () => { + console.log( + `API server listening on: http://${config.server.host}:${config.server.port}${config.swagger.basePath}` + ); + }); + } +} + +const Server = new App(); -export default app; +export default Server.app; diff --git a/backend/test/api/controllers/index.test.js b/backend/test/api/controllers/index.test.js index 463469e..4a0ecdc 100644 --- a/backend/test/api/controllers/index.test.js +++ b/backend/test/api/controllers/index.test.js @@ -3,7 +3,7 @@ import mongoose from 'mongoose'; // Clear database before each test case before(done => { Object.keys(mongoose.connection.collections).map(collection => - mongoose.connection.collections[collection].remove(() => {}), + mongoose.connection.collections[collection].remove(() => {}) ); done(); });