Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jobs:
docker:
- image: circleci/node:12.13.0
- image: circleci/mongo:4.2.1
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# ongdev-api
[![Coverage Status](https://coveralls.io/repos/github/OngDev/api-gateway/badge.svg?branch=master)](https://coveralls.io/github/OngDev/api-gateway?branch=master)

[![Coverage Status](https://coveralls.io/repos/github/OngDev/api-gateway/badge.svg?branch=master)](https://coveralls.io/github/OngDev/api-gateway?branch=master)
63 changes: 48 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import helmet from 'helmet';
import morgan from 'morgan';
import { join } from 'path';
import rfs from 'rotating-file-stream';
import mongoose from 'mongoose';
import session from 'express-session';
import errorHandler from 'errorhandler';
import dotenv from 'dotenv';
import logger from './src/logger/logger';
import dbConfig from './src/configs/db.config';
import connectDatabase from './src/configs/db.config';

mongoose.Promise = global.Promise;
/* istanbul ignore next */
dotenv.config();

// configure isProduction variable
const isProduction = process.env.NODE_ENV === 'production';

// defining the Express app
const app = express();
Expand All @@ -32,18 +38,20 @@ const accessLogStream = rfs('access.log', {
// adding morgan to log HTTP requests
app.use(morgan('combined', { stream: accessLogStream }));

// Connecting to the database
mongoose
.connect(dbConfig.connectionString, {
useNewUrlParser: true,
})
.then(() => {
logger.info('Successfully connected to the database');
})
.catch((err) => {
logger.info(`Could not connect to the database. Exiting now...\n${err}`);
process.exit();
});
// configure session
app.use(session({
secret: 'vedgno',
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false,
}));

// connect to mongo
connectDatabase();

if (!isProduction) {
app.use(errorHandler());
}

app.get('/', (req, res) => {
logger.info('GET /');
Expand All @@ -58,6 +66,31 @@ app.get('*', (req, res) => {
res.send('App works!!!!!');
});

// Error handlers & middlewares
if (!isProduction) {
app.use((err, req, res) => {
res.status(err.status || 500);

res.json({
errors: {
message: err.message,
error: err,
},
});
});
}

app.use((err, req, res) => {
res.status(err.status || 500);

res.json({
errors: {
message: err.message,
error: {},
},
});
});

// starting the server
app.listen(3001, () => {
logger.info('listening on port 3001');
Expand Down
Loading