-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (62 loc) · 1.89 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
'use strict';
const koa = require('koa')
, app = koa()
, json = require('koa-json')
, serve = require('koa-static')
, handlebars = require('koa-handlebars')
, bodyParser = require('koa-bodyparser')
, statuses = require('statuses');
// -------------------------------------------
// custom status codes
// -------------------------------------------
statuses['440'] = 'The oath2 response token type is not "bearer".';
// -------------------------------------------
// middleware
// -------------------------------------------
// error handling
app.use(function *(next) {
try {
yield next;
} catch (err) {
this.status = err.status || 500;
this.body = err.message;
this.app.emit('error', err, this);
}
});
// app.use(function *(next) {
// //This will set status and message
// this.throw('Error Messagezzz', 500);
// });
// app.use(function *(next) {
// //This will only set message
// throw new Error('Error Messagerrrr');
// });
// for json responses : with pretty toggle
app.use(json({ pretty: false, param: 'pretty' }));
// basic body parser
app.use(bodyParser());
// serve static files
app.use(serve(__dirname + '/public'));
// handlebars config
app.use(handlebars({
extension: ['hbs', 'handlebars'],
defaultLayout: 'index',
layoutsDir: 'app/templates',
viewsDir: 'app/templates/views',
partialsDir: 'app/templates/partials'
}));
// webpack dev server for LOCAL DEV ONLY
if (process.env.NODE_ENV !== 'production') {
let webpackDevServer = require('koa-webpack-dev');
app.use(webpackDevServer({
config: './webpack.config.js'
}));
}
// -------------------------------------------
// init app
// -------------------------------------------
// initialize routes, set port
const routes = require('./app/routes')(app).next()
, port = process.env.PORT || 5000;
app.listen(port);
console.log('Koa listening on port ' + port);