-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (100 loc) · 3.46 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
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
const Koa = require('koa')
const app = new Koa()
const path = require('path')
const json = require('koa-json')
const onerror = require('koa-onerror')
const koaBody = require('koa-body');
const logger = require('koa-logger')
const render = require('koa-art-template')
const serve = require('koa-static')
const main = require('./routes/main')
const api = require('./routes/api')
const admin = require('./routes/admin')
const session = require('koa-session')
// error handler
onerror(app)
// middlewares
app.keys = ['fanyihui']
const CONFIG = {
key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
/** (number || 'session') maxAge in ms (default is 1 days) */
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 10000000,
autoCommit: true, /** (boolean) automatically commit headers (default true) */
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) */
rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
}
app.use(session(CONFIG, app))
app.use(koaBody({
multipart: true,
formidable: {
maxFileSize: 200*1024*1024 // 设置上传文件大小最大限制,默认2M
}
}))
app.use(json())
app.use(logger())
app.use(serve(__dirname + '/public'))
app.use(serve(__dirname + '/static'))
render(app, {
root: path.join(__dirname, 'views'),
extname: '.html',
debug: process.env.NODE_ENV !== 'production'
});
// routes
let a = []
app.use(async (ctx, next) => {
let ip = ctx.request.get("X-Real-IP") || ctx.request.get("X-Forwarded-For") || ctx.request.ip
console.log('ip ' + ip + ' 访问了 ' + ctx.originalUrl + ' 访问时间: ' + new Date())
a.push(ip)
a = [...new Set(a)]
ctx.request.peopleNums = a.length
await next()
})
//session拦截
app.use(async (ctx, next) => {
const allowpage = ['/admin']
if (allowpage.includes(ctx.originalUrl)) {
} else if (ctx.originalUrl.startsWith('/admin/')){
if (ctx.session.isAdmin == null || ctx.session.isAdmin == undefined) {
ctx.redirect('/admin')
return
}
}
await next()
})
// app.use(async (ctx, next) => {
// try {
// await next()
// if (ctx.status === 404) {
// ctx.throw(404);
// }
// } catch (err) {
// console.error(err.stack);
// const status = err.status || 500;
// ctx.status = status;
// if (status === 404) {
// ctx.render('main/404.html')
// } else if (status === 500) {
// ctx.body = '500'
// }
// }
// })
app.use(main.routes(), main.allowedMethods())
app.use(api.routes(), api.allowedMethods())
app.use(admin.routes(), admin.allowedMethods())
// 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`)
})
// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
})
module.exports = app