-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
207 lines (169 loc) · 5.15 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import koa from 'koa'
import ip from 'ip'
import Router from 'koa-router'
import StaticRouter from 'koa-static'
import KoaBody from 'koa-body'
import Session from 'koa-generic-session'
import RedisStore from 'koa-redis'
import convert from 'koa-convert'
import historyApiFallback from 'koa-connect-history-api-fallback'
import { get_user_info, set_user_password, check_user_password } from './server/user-info'
var myRouter = new Router();
myRouter.all('/', function *(next)
{
try
{
yield next;
}
catch(err)
{
console.log('/ server error = ', err);
this.status = err.status;
this.body = err.message;
}
});
myRouter.get('/server/aaa', function *(next)
{
console.log('/aaa 403');
//this.throw(403, 'What are you doing? a');
this.body = 'What are you doning?';
});
myRouter.all('/server/bbb', function *(next)
{
console.log( '/bbb body = ', this.request.body );
//this.throw(403, 'What are you doing? a');
this.body = 'What are you doning?';
});
myRouter.get('/server/getuserinfo/:id', function *(next)
{
console.log('/getuserinfo params = ', this.params.id);
var result = yield get_user_info( this.params.id );
this.response.set('Access-Control-Allow-Origin', '*');
this.body = JSON.stringify( result[0] );
console.log('/getuserinfo finish');
});
//curl -l -H "Content-type: application/json" -X POST -d '{"username":"YYQ","password":"1234","remember":true}' http://localhost:3001/server/checkuserlogin
myRouter.post('/server/checkuserlogin', function *(next)
{
console.log( '/checkuserlogin this.params = ', this.params, ' body = ', this.request.body, ' session = ', this.session );
let userinfo = JSON.parse( this.request.body );
let result = yield check_user_password( userinfo.username, userinfo.password );
if( result.id && userinfo.remember )
{
console.log('/checkuserlogin set cookies id = ', result.id);
this.cookies.set( 'id', result.id, {signed: true} );
//this.session
}
else
{
this.cookies.set('id');
this.cookies.set('id.sig');
}
this.response.set('Access-Control-Allow-Origin', '*');
this.body = JSON.stringify( result );
console.log('/checkuserlogin finish, body = ', this.body);
});
myRouter.all('/server/userlogout', function *(next)
{
let userid = this.cookies.get('id', {signed: true});
console.log( '/userlogout userid = ', userid );
this.cookies.set('id');
this.cookies.set('id.sig');
//this.session
this.response.set('Access-Control-Allow-Origin', '*');
this.body = '{}';
});
myRouter.get('/server/test/:id', function *(next)
{
console.log('/test');
this.body = 'test page, params = ' + this.params.id + " dir = " + __dirname;
});
myRouter.all('/server/', function *(next)
{
console.log('/ccc 1');
var cookie_name = this.cookies.get('name', { signed: true });
console.log('/ccc cookie_name = ' + cookie_name);
if( !cookie_name || 1 > cookie_name.length )
{
this.cookies.set('name', 'tubie', { signed: true });
}
console.log('/ccc 2');
this.body = 'Hello World cookie_name=' + cookie_name + this.url + " " + this.request.path + " " + this.request;
});
/*
// koa-router 中间件示例,目前本框架中没有调用
app.use(function *(next)
{
var start = new Date;
try
{
yield next;
}
catch(err)
{
log.error('server error', err);
this.status = err.status;
this.body = err.message;
}
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(function *(next)
{
if( this.url == '/aaa' )
{
console.log('/aaa 403');
this.throw(403, 'What are you doing?');
}
else
{
yield next;
}
});
*/
var app = koa();
//var Keygrip = require('keygrip')
//app.keys = new Keygrip(['im a newer secret', 'i like turtle'], 'sha256');
app.keys = ['im a newer secret', 'i like turtle'];
/*
app.use(Session({
key: 'test.sid',
store: RedisStore({
host: ,
port: ,
password:
}),
ttl: 24*3600*1000, //单位为 ms,可以直接写 毫秒数
cookie: {
httpOnly: true,
path: '/',
overwrite: true,
signed: true,
maxAge: null //one hour in ms
}
}));
*/
app.on('error', function(err){
console.log('server error = ', err);
});
app.use(KoaBody({formidable:{uploadDir: __dirname}}));
app.use(myRouter.routes());
// This rewrites all routes requests to the root /index.html file
// (ignoring file requests). If you want to implement isomorphic
// rendering, you'll want to remove this middleware.
console.log('historyApiFallback = ' + historyApiFallback)
console.log('convert = ' + convert)
let koa_Fallback = historyApiFallback({
verbose: false,
index: 'index.html'
});
let koa_Fallback_g = convert( koa_Fallback )
console.log('koa_Fallback = ' + koa_Fallback)
console.log('koa_Fallback_g = ' + koa_Fallback_g)
//使客户端 url 重定向到 index.html
app.use(koa_Fallback);
//加载客户端文件
app.use(StaticRouter(__dirname + '/client'));
app.listen(3001);
const localip = ip.address()
console.log(`koa web server running at: http://${localip}:3001/, dirname = ${__dirname}`);