-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-server.js
120 lines (94 loc) · 3.61 KB
/
test-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
const Koa = require('koa');
const fs = require('fs');
const path = require('path');
const serve = require('koa-static');
// 注意require('koa-router')返回的是函数:
const router = require('koa-router')();
const app = new Koa();
console.log(path.join(__dirname, 'views'));
const main = serve(path.join(__dirname, '/views'));
const bodyParser = require('koa-bodyparser');
// 静态文件处理
app.use(main);
// 解析request的body
app.use(bodyParser());
// log request URL:
app.use(async (ctx, next) => {
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
await next();
});
//定位到教师登陆后的主界面'/course'
router.get('/course', async (ctx, next) => {
console.log('login router');
//console.log(__dirname);
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/teacher/courseList.html');
});
// add url-route:
router.get('/user/login', async (ctx, next) => {
console.log('login router');
//console.log(__dirname);
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/login.html');
});
router.get('/user/change_password', async (ctx, next) => {
console.log('login router');
//console.log(__dirname);
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/teacher/changePasswordPage.html');
});
router.patch('/api/user/password', async (ctx, next) => {
console.log('change password check...');
//console.log(__dirname);
ctx.cookies.set('key', '3w4e5r6tyuifcgvhbjnkmlvg');
ctx.response.status = 201;
console.log('ctx.request:',ctx.request);
console.log('ctx.request.body:',ctx.request.body);
});
router.get('/course/:course_id', async (ctx, next) => {
console.log('login router');
console.log('getting a course...');
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/teacher/courseDetail.html');
});
//学生名单界面
router.get('/course/:course_id/course_member', async (ctx, next) => {
console.log('login router');
console.log('getting students names...');
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/teacher/studentNameLlistPage.html');
});
//课程签到历史界面
router.get('/course/:course_id/checkin_student', async (ctx, next) => {
console.log('login router');
console.log('getting attendance history...');
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/teacher/checkAttendancePage.html');
});
router.post('/api/users/session', async (ctx, next) => {
console.log('login check');
ctx.cookies.set('key', '3w4e5r6tyuifcgvhbjnkmlvg', {
maxAge: 3600*1000 ,
httpOnly: false
}
);
console.log('ctx.request:',ctx.request);
console.log('ctx.request.body:',ctx.request.body);
console.log('user id:',ctx.request.body.user_id,' password:',ctx.request.body.password);
/*ctx.response.type = 'json';
ctx.response.body = {message: 'empty username or password'};*/
ctx.response.status = 201;
});
router.delete('/api/users/session', async (ctx, next) => {
console.log('/api/users/session');
ctx.response.status = 204;
})
router.get('/', async (ctx, next) => {
// ctx.response.body = '<h1>Index</h1>';
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./views/html/initial.html');
});
// add router middleware:
app.use(router.routes());
app.listen(8000);
console.log('app started at port 8000...');