-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (38 loc) · 1.05 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
var app = require('koa')()
, logger = require('koa-logger')
, json = require('koa-json')
, views = require('koa-views')
, onerror = require('koa-onerror')
, router = require('koa-router')();
var index = require('./routes/index');
var users = require('./routes/users');
// error handler
onerror(app);
// global middlewares
app.use(views('views', {
root: __dirname + '/views',
default: 'jade'
}));
app.use(require('koa-bodyparser')());
app.use(json());
app.use(logger());
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(require('koa-static')(__dirname + '/public'));
// routes definition
app
.use(router.routes())
.use(router.allowedMethods());
router.use('/',index.routes());
router.use('/users',users.routes());
// app.use(index.routes(), index.allowedMethods());
// app.use(users.routes(), users.allowedMethods());
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
module.exports = app;