-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
73 lines (60 loc) · 2.16 KB
/
server.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
/*eslint no-console: ["error", { allow: ["log"] }] */
/* eslint-disable no-unused-vars*/
// dependencies ----------------------------------------------------
import express from 'express';
import async from 'async';
import config from './config';
import routes from './routes';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import mongo from './libs/mongo';
import redis from './libs/redis';
import queue from './libs/queue';
import notifications from './libs/notifications';
import aws from './libs/aws';
// import events lib to instantiate CRN Emitter
import events from './libs/events';
// configuration ---------------------------------------------------
mongo.connect();
redis.connect(config.redis, () => {
queue.connect(() => {
console.log('Resque connected');
});
});
let app = express();
app.use((req, res, next) => {
res.set(config.headers);
res.type('application/json');
next();
});
app.use(morgan('short'));
app.use(bodyParser.json());
// routing ---------------------------------------------------------
app.use(config.apiPrefix, routes);
// error handling --------------------------------------------------
app.use(function(err, req, res, next) {
res.header('Content-Type', 'application/json');
var send = {'error' : ''};
var http_code = (typeof err.http_code === 'undefined') ? 500 : err.http_code;
if (typeof err.message !== 'undefined' && err.message !== '') {
send.error = err.message;
} else {
if(err.http_code == 400){
send.error = 'there was something wrong with that request';
}else if(err.http_code == 401){
send.error = 'you are not authorized to do that';
}else if(err.http_code == 404){
send.error = 'that resource was not found';
}else{
send.error = 'there was a problem';
}
}
res.status(http_code).send(send);
});
// start background tasks
notifications.initCron();
aws.batch.initCron();
// start server ----------------------------------------------------
app.listen(config.port, () => {
console.log('Server is listening on port ' + config.port);
});