-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (50 loc) · 1.41 KB
/
server.js
File metadata and controls
58 lines (50 loc) · 1.41 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
/**
* Created by zhoujun on 2017/2/6.
* email :zhoujun247@gmail.com
*/
import Koa from 'koa';
import Router from 'koa-router';
import messageRouter from './src/router/messageRouter';
import bodyparser from 'koa-bodyparser';
const session = require('koa-session')
const convert = require('koa-convert');
const path = require('path');
var send = require('koa-send');
const app = new Koa();
const ENV = 'test'
app.keys = ['some secret hurr'];
var CONFIG = {
key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
maxAge: 86400000, /** (number) maxAge in ms (default is 1 days) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
};
app.use(convert(session(CONFIG,app)));
app.use(async(ctx,next)=>{
if(ctx.path == '/login' && !ctx.session.login ){
await send(ctx, 'index.html');
}else{
if(ctx.path == '/login'){
ctx.redirect('/');
}
if(ctx.session.login || ctx.path == '/post_login'){
await next();
}else{
ctx.redirect('login');
}
}
});
app.use(bodyparser());
app.use(messageRouter.routes(),messageRouter.allowedMethods());
if(ENV == 'test'){
app.use(async function (ctx, next){
await send(ctx, 'index.html');
})
}else{
app.use(async function (ctx, next){
await send(ctx, 'index-production.html');
})
}
app.listen(4001);
console.log('listen:4000')