-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
48 lines (40 loc) · 1.31 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
'use strict';
var messages = require('./controllers/messages');
var compress = require('koa-compress');
var logger = require('koa-logger');
var serve = require('koa-static');
var route = require('koa-route');
var koa = require('koa');
var path = require('path');
var app = module.exports = koa();
var moment = require('moment');
var winston = require('winston');
var winstonLogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(
{
formatter: function(options) {
return moment().format() +' # '+ options.level.toUpperCase() +': '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
}
)
]
});
// Logger
app.use(logger());
app.use(route.get('/', messages.home));
app.use(route.get('/messages', messages.list));
app.use(route.get('/messages/:id', messages.fetch));
app.use(route.post('/messages', messages.create));
app.use(route.get('/async', messages.delay));
// Serve static files
app.use(serve(path.join(__dirname, 'public')));
app.use(serve(path.join(__dirname, '.')));
// Compress
app.use(compress());
if (!module.parent) {
app.listen(3000);
console.log('listening on port 3000');
winstonLogger.info('listening on port 3000');
}