-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
63 lines (55 loc) · 2.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @file Defines the basic configuration of the Express Framework @see {@link https://expressjs.com/} and Mongoose
* ORM @see {@link https://mongoosejs.com} connection to MONGODB database. Then it exports the config as module.
* @author Gabriel <[email protected]> <20/06/2020 06:37am>
* @since 0.1.0
* Last Modified: Gabriel <[email protected]> <13/07/2020 06:17pm>
*/
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routeHandler = require('./src/routes');
const {
ApiError,
InternalError,
NotFoundError,
} = require('./src/utilities/core/ApiError');
module.exports = (config) => {
const app = express();
console.log(config.mongodb);
// initialize mongodb connection with mongoose
mongoose
.connect(config.mongodb.dsn, config.mongodb.options)
.then(() => {
config.logger.info('Successfully connected to MongoDb');
})
.catch((error) =>
config.logger.info(`Could not connect to MongoDb\n${error}`)
);
mongoose.set('useFindAndModify', false); // fix findOneAndUpdate() deprecation warning
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/**
* Adds application routes middleware from the routes index which groups all routes together
*/
app.use('/api/v1', routeHandler(config));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(new NotFoundError('Resource Not Found'));
});
// error handler
// eslint-disable-next-line no-unused-vars
app.use(function (err, req, res, next) {
// Checks if err is thrown by us and handled to the ApiError Class, if not we throw and handle an internal server error
if (err instanceof ApiError) {
// TODO: log error to a file
ApiError.handle(err, res);
} else {
// TODO: log error to a file
ApiError.handle(new InternalError(), res);
}
// log error to the console for debugging purpose
config.logger.error(`${err} app----`);
});
return app;
};