-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
145 lines (122 loc) · 3.6 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Module dependencies
*/
const http = require('http');
const debug = require('debug')('application:server');
const swaggerUI = require('swagger-ui-express');
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const yaml = require('yamljs');
const Redis = require('ioredis');
const cors = require('cors');
require('dotenv').config();
// will use in the future
// const swaggerJSDocs = require('swagger-jsdoc');
const swaggerJS = yaml.load('./openAPI/openapi.yaml');
const app = express();
app.use(cors());
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerJS));
app.use(logger('dev'));
app.use(express.json());
/**
* This function is to calculate the total cost of a route,
* this is the main fucntion
* @param {req} object The parameter contains the content of the request to the API
*/
app.use(function (err, req, res, next) {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
// Handle the error here
console.error('Bad JSON');
res.status(400).send({ error: 'JSON parse error' });
}
// Pass the error to the next middleware if it wasn't a JSON parse error
next(err);
});
app.use(express.urlencoded({ extended: false }));
const mongoDB = process.env.URL_DB;
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
});
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
/**
* Connect to reddis client locally
*/
const redis = new Redis(6379);
redis.on('connect', () => console.log('Connected to Redis'));
redis.on('error', (err) => console.error('Redis error encountered', err));
exports.redis = redis;
app.use('/', require('./routes/index.js'));
/**
* redirect 404 errors in ExpressJS
*/
app.use((req, res, next) => {
res.status(404).send({ error: 'Not found. Please following this guide https://github.com/SneyderHOL/Quick_Project' });
next();
});
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort (val) {
const port = parseInt(val, 10);
if (isNaN(port)) return val;
if (port >= 0) return port;
return false;
}
/**
* This function is to calculate the total cost of a route,
* this is the main fucntion
* @param {req} object The parameter contains the content of the request to the API
*/
function onError (error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
default:
throw error;
}
}
/**
* This function is to calculate the total cost of a route,
* this is the main fucntion
* @param {req} object The parameter contains the content of the request to the API
*/
function onListening () {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
module.exports = { app, mongoose };