Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
express app startup refactored πŸ‹πŸΎβ€β™‚οΈ
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-kas committed Jul 6, 2019
1 parent b3c075d commit 9d4614b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
99 changes: 59 additions & 40 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion backend/test/api/controllers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 9d4614b

Please sign in to comment.