-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (62 loc) · 2.3 KB
/
app.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
const Koa = require('koa')
const app = new Koa()
// const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
// import controller
// const main = require('./controller/index');
// const student = require('./controller/student');
// const teacher = require('./controller/teacher');
// const admin = require('./controller/admin');
// import api
const studentApi = require('./apis/students');
const teacherApi = require('./apis/teachers');
const adminApi = require('./apis/admins');
const paperApi = require('./apis/papers');
const enrollApi = require('./apis/enroll');
const timeLineApi = require('./apis/timeLine');
// set key for cookies
// app.keys = ['$#pVR7o^QTA6EvvQ', 'TQKj$V$hz4!5d9k9'];
// error handler
onerror(app)
// middlewares
app.use(bodyparser())
app.use(logger())
// set app.context
app.context.jwtBody = {};
// test here to show excel
// var obj = excel.parse("./text_files/" + "students.xlsx");
// console.log(JSON.stringify(obj));
// logger
app.use(async (ctx, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
// ctx.response.set("Access-Control-Allow-Origin", "localhost:8080");
})
// app.use(async (ctx, next) => {
// await next();
// console.log("我执行啦!");
// // ctx.response.header["Access-Control-Allow-Headers"] = "Content-Type,Content-Length, Authorization, Accept,X-Requested-With";
// // ctx.response.header["Access-Control-Allow-Methods"] = "localhost:8080";
// console.log(ctx.response.header);
// })
// controller
// app.use(main.routes(), main.allowedMethods());
// app.use(teacher.routes(), teacher.allowedMethods());
// app.use(student.routes(), student.allowedMethods());
// app.use(admin.routes(), admin.allowedMethods());
// api routes
app.use(studentApi.routes(), studentApi.allowedMethods());
app.use(teacherApi.routes(), teacherApi.allowedMethods());
app.use(adminApi.routes(), adminApi.allowedMethods());
app.use(paperApi.routes(), paperApi.allowedMethods());
app.use(enrollApi.routes(), enrollApi.allowedMethods());
app.use(timeLineApi.routes(), timeLineApi.allowedMethods());
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});
module.exports = app