-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (42 loc) · 1.26 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
const Koa = require('koa');
const Router = require('koa-router');
const session = require('koa-session');
const next = require('next');
const Redis = require('ioredis');
const sessionStore = require('./server/sessionStore');
const auth = require('./server/auth');
const routeConfig = require('./server/route');
const api = require('./server/api');
const koaBody = require('koa-body');
const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();
const redis = new Redis();
app.prepare().then(() => {
const server = new Koa();
const router = new Router();
server.use(koaBody());
// 为cookie加密
server.keys = ['ff8789sb'];
const SESSION_CONFIG = {
key: 'key',
store: new sessionStore(redis)
};
server.use(session(SESSION_CONFIG, server));
// 授权中间件
auth(server);
// api中间件
api(server);
// 使用路由中间件
routeConfig(router);
server.use(router.routes());
// 默认中间件
server.use(async(ctx, next) => {
ctx.req.session = ctx.session;
await handle(ctx.req, ctx.res);
ctx.respond = false;
});
server.listen(3000, () => {
console.log('koa server listening on 3000');
});
});