-
Notifications
You must be signed in to change notification settings - Fork 120
/
app.js
112 lines (100 loc) · 2.92 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
const Koa = require('koa');
const path = require('path');
const static = require('koa-static');
const Router = require('koa-router');
const body = require('koa-body');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const views = require('koa-views');
const ejs = require('ejs');
const tokenStore = require('./utils/token/koa-token-store');
const cookieStore = require('./utils/cookie/koa-cookie-store');
const app = new Koa();
const tokenConfig = require('./utils/config').sessToken;
const cookieConfig = require('./utils/config').sessCookie;
const sessionConfig = require('./utils/config').project;
// 后台管理系统-接口
const manageRouter = require('./router/manage');
// 前台页面系统-路由
const webRouter = require('./router/web');
// 前置中间件-获取用户ip,并且加工成可以使用的ipv4
const filterUserIp = require('./middleware/userIp');
// 后置中间件-路径纠错,处理用户访问路径
const routerCorrect = require('./middleware/router');
// 前置中间件-检测是否关闭了网站
const webService = require('./middleware/service');
// 前置中间件-用于处理纯静态化路径
const openStatic = require('./middleware/static');
// 前置中间件-用于修正静态化后没有找到的文件路径,恢复原路径
const cacheCorrect = require('./middleware/cacheCorrect');
// gzip
app.use(compress({
threshold: 1024
}));
// 检测是否关闭网站
app.use(webService())
// 加工用户IP
app.use(filterUserIp())
// 纯静态检查
app.use(openStatic())
// 模板引擎ejs
app.use(views(
path.resolve(__dirname, './static/template'),
{ map: {html: 'ejs' }}
))
// 静态文件
app.use(static(
path.join( __dirname, './static'),
{
maxage: 2592000000
}
))
// 矫正静态化以后,没有找到的文件,路由回正,走查询数据库
app.use(cacheCorrect())
// 参数解析
app.use(bodyParser());
// session cookie
app.use(cookieStore({
host: cookieConfig.host,
port: cookieConfig.port,
dbName: cookieConfig.dbName,
collection: cookieConfig.collection,
maxAge: cookieConfig.maxAge,
}))
// session token
app.use(tokenStore({
host: tokenConfig.host,
port: tokenConfig.port,
dbName: tokenConfig.dbName,
collection: tokenConfig.collection,
maxAge: tokenConfig.maxAge,
}))
// 页面系统 路由
app.use(webRouter.routes());
// 管理系统 路由
app.use(manageRouter.routes());
// url矫正
app.use(routerCorrect())
// 静态文件 - 处理矫正后的文件访问路径
app.use(static(
path.join( __dirname, './static')
))
// 静态文件没有找到
app.use(async (ctx, next) => {
// 错啦,没加模板模板路径
if(ctx.request.method === 'GET'){
ctx.request.url = '/404/index.html';
}
if(ctx.request.method === 'POST'){
ctx.status = 403;
ctx.body = '';
}
return next()
})
// 接404页面
app.use(static(
path.join( __dirname, './static')
))
app.listen(9999, () => {
console.log('运行成功,端口 9999');
})