-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (99 loc) · 3.32 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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const database = require('./database/mongo_connection');
const debug = require('debug')('semut-svc:server');
const http = require('http');
const commonMsg = require('./configs/common_messages.json');
let app = express();
const cors = require('cors');
app.use(cors());
/** setup express **/
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/** get mongodb connection pool* */
database.connect().then(db =>{
/** if error **/
app.use((err, req, res, next) => {
res.status(200).send(commonMsg.service_not_responding);
});
/** init server **/
let port = normalizePort(process.env.PORT || '3049');
app.set('port', port);
let server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
let port = parseInt(val, 10);
if (isNaN(port)) return val;
if (port >= 0) return port;
return false;
}
function onError(error) {
if (error.syscall !== 'listen') throw error;
let bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
/** export mongodb connection **/
app.db = db;
module.exports = app;
/** run mq service**/
const rmqConnection = require('./rmq/connection');
rmqConnection.connect();
/** define routes**/
let index = require('./routes/index');
let users = require('./routes/users');
let posts = require('./routes/posts');
let trayek = require('./routes/trayek');
let userv2 = require('./routes/users_v2');
let cctv = require('./routes/cctv');
let postv2 = require('./routes/post_v2');
let publicTrans = require('./routes/public_trans');
let notif = require('./routes/notif');
let bike = require('./routes/bike');
app.use('/', index);
app.use('/users', users);
app.use('/posts', posts);
app.use('/trayek', trayek);
app.use('/api/v2/users', userv2);
app.use('/api/v2/cctv', cctv);
app.use('/api/v2/post', postv2);
app.use('/api/v2/publictrans', publicTrans);
app.use('/api/v2/notif', notif);
app.use('/api/v2/bike', bike);
/** if route not found**/
app.use((req, res) => {
res.status(200).send(commonMsg.routes_not_found);
});
}).catch(err=>{
console.log(err);
});