-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 3.67 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
*
* @module Index
* @fileOverview Entry point to the application.
* @description This is the entry point to the application. Application has a MVC architecture and all the controller modules are routed from here. Base URL: <b>http://localhost:3000/</b> to be appended with each controller node and specific API endpoint.
* @author Hanumanthu Indrakanti <ambika.amruta.pani@gmail.com>
* @version 0.1.0
* @returns {StandardResponse} All APIs return response in a standard format
*/
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const constants = require('./utils/app.constants');
const logger = require('./utils/logger').winston;
// ----------API Middleware Setup--------------//
const app = express();
app.use('*', (request, response, next) => {
if (process.env.NODE_ENV === 'development') {
logger.info('%s is called', request.originalUrl);
}
next();
});
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/ping', (request, response) => {
response.json('Bruce is alive and kicking');
});
const cityRouter = require('./controllers/city.controller').cityRouter;
app.use('/city', cityRouter);
// Error routes to be placed at the bottom of routes
app.use((request, response) => {
response.status(404).send(constants.ERROR_CODES.RESOURCE_NOT_FOUND);
});
app.use((error, request, response, next) => {
if (error.name === 'UnauthorizedError' && error.code != 'permission_denied') {
response.status(401).send(constants.ERROR_CODES.UNAUTHORIZED);
return;
}
return next(error);
});
// global error handler
app.use((error, request, response, next) => {
logger.error('%o', error);
const errorResponse = JSON.parse(JSON.stringify(constants.ERROR_CODES.SERVER_ERROR));
if (process.env.NODE_ENV === 'development') {
errorResponse.error.stack = error.stack || error;
} else {
// do not throw the error stack to end user in production
errorResponse.error.message = error.message;
}
response.status(500).send(errorResponse);
});
let server;
try {
server = app.listen(process.env.API_PORT, process.env.HOST, error => {
if (error) {
logger.error('connection error %o', error);
throw error;
}
logger.info('Listening on localhost: %s', process.env.API_PORT);
});
} catch (error) {
logger.error('Error while connecting: %o', error);
// process.exit(1);
}
process.on('SIGINT', () => {
logger.crit('SIGINT signal received at %s', new Date());
// Stop server from accepting new connections
if (server) {
server.close(error => {
if (error) {
logger.emerg('server is closing with the following error: %o', error);
process.exit(1);
}
});
}
// Close all db connections here
});
process.on('uncaughtException', exception => {
logger.error('exception: %o', exception);
});
process.on('unhandledRejection', (reason, p) => {
logger.info('Unhandled Rejection at: %o, reason: %o', p, reason);
// application specific logging, throwing an error, or other logic here
});
/**
* @typedef StandardResponse
* @property {Object} response
* @property {Boolean} success returns true if API is executed successfully passing all error checks else returns false with error code and message.
* @property {Object} [error] only if success is false
* @property {Number} [error.code]
* @property {String} [error.message]
* @property {String} [error.description]
* @property {String} [error.stack]
* @property {Object} [result] returns the API response if success is true
*/