forked from lestoni/dashy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·141 lines (111 loc) · 2.93 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
'use strict';
/**
* Module dependencies.
*/
const http = require('http');
const app = require('koa')();
const koa = require('koa-router')();
const logger = require('koa-logger');
const json = require('koa-json');
const views = require('koa-views');
const onerror = require('koa-onerror');
const debug = require('debug')('dashy:server');
const mongoose = require('mongoose');
const session = require('koa-generic-session');
const MongoStore = require('koa-generic-session-mongo');
const validator = require('koa-validate');
const flash = require('koa-flash');
const router = require('./routes');
const config = require('./config');
const bootstrap = require('./boot');
validator(app);
app.keys = config.SESSION_KEYS;
/**
* Get port from environment and store in Express.
*/
const port = config.PORT;
// connect to MongoDB
mongoose.connect(config.MONGODB.URL, config.MONGODB.OPTS);
// MongoDB connection error Handler
mongoose.connection.on('error', () => {
debug('responding to MongoDB connection error');
console.error('MongoDB connection error. Please make sure MongoDB is running');
process.exit(1);
});
bootstrap()
.then((info) => {
console.log('Bootstrapping successful');
}).catch((err) => {
console.log(err);
process.exit(1);
});
app.use(session({
store: new MongoStore({ url: config.MONGODB.URL })
}));
app.use(flash());
// global middlewares
app.use(views('views', {
root: __dirname + '/views',
default: 'ejs'
}));
app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());
app.use(function *(next){
const start = new Date;
yield next;
const ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(require('koa-static')(__dirname + '/public'));
// mount root routes
app.use(router.routes());
app.on('error', function(err, ctx){
console.log(err);
logger.error('server error', err, ctx);
});
/**
* Create HTTP server.
*/
const server = http.createServer(app.callback());
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Event listener for HTTP server "error" event.
*/
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);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
module.exports = app;