app.js
1.4 KB
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
/**
*
* @
*/
'use strict';
import http from 'http';
import Koa from 'koa';
import mount from 'koa-mount';
import serve from 'koa-static';
import body from './middleware/yoho-koa-body';
import convert from 'koa-convert';
import session from 'koa-session';
import Socket from 'socket.io';
import config from './config/config';
import webApp from './apps/web';
import api from './apps/api';
import ws from './lib/ws';
import errorHandle from './middleware/error-handle';
const port = process.env.PORT || config.port;
const app = new Koa();
const server = http.createServer(app.callback());
const io = new Socket(server);
ws.init(io);
app.keys = ['yoho-node-ci secret'];
app.use(errorHandle(app));
app.use(serve(__dirname + '/public'));
app.use(convert(session({}, app)));
app.use(convert(body({
multipart: true,
queryString: {
plainObjects: true
}
})));
app.use(mount('/api', api));
app.use(mount('/', webApp));
// app.on('error', function(err, ctx) {
// console.log(err);
// switch (ctx.accepts('json', 'html', 'text')) {
// case 'json':
// ctx.body = {
// code: 400
// };
//
// break;
// case 'html':
// console.log(ctx);
// ctx.redirect('/500');
// break;
// default:
// break;
// }
// });
server.listen(port, () => {
console.log(`app started in ${port}`);
});