-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (41 loc) · 1.08 KB
/
index.js
File metadata and controls
49 lines (41 loc) · 1.08 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
"use strict";
import http from 'http';
import path from 'path';
import co from 'co';
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import render from 'koa-ejs';
import serve from './modules/push';
import routes from './routes';
import config from './config.json';
//设置参数
process.env.PORT = config.io.port + parseInt(process.env.NODE_APP_INSTANCE || 0);
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const app = new Koa();
//错误处理
app.use(async(ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = {message: err.message};
}
});
//中间件
app.use(bodyParser());
if (process.env.NODE_ENV === 'development') {
render(app, {
root: path.join(__dirname, 'views'),
layout: false,
viewExt: 'ejs',
cache: false
});
app.context.render = co.wrap(app.context.render);
}
//路由
routes(app);
//创建http服务
const server = http.createServer(app.callback());
server.listen(process.env.PORT);
//创建socket.io服务
serve.create(server);