-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (76 loc) · 2.24 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
const Koa = require('koa')
const static = require('koa-static')
const views = require('koa-views')
const router = require('./routers')
const logger = require('koa-logger')
const body = require('koa-body')
const session = require('koa-session')
const { join } = require('path')
const compress = require('koa-compress')
const app = new Koa
app.keys = ['胖虎是个大帅比']
// 这是session的配置对象
const CONFIG = {
key: 'Sid',
maxAge: 36e5, // 36后面5个0 的 科学计数法
overwrite: true, // 是否覆盖
httpOnly: true, // 是否http可见
signed: true, // 是否签名
rolling: true // 是否刷新
}
app.use(logger())
// 注册 资源压缩模块
app.use(compress({
// filter (content_type) {
// return /text/i.test(content_type) // 检测到匹配的文件类型 为 true的 被压缩,如果希望所有的 都被 压缩,那么 删除 这个配置
// },
threshold: 2048, // size 大于 2kb的 都会被 压缩
flush: require('zlib').Z_SYNC_FLUSH
}))
// 注册session
app.use(session(CONFIG, app)) // 手动把app传进去
// 配置 koa-body 处理 post请求 数据
app.use(body())
// 注册 静态资源目录
app.use(static(join(__dirname, 'public')))
// 注册 视图 模块
app.use(views(join(__dirname, 'views'), {
extension: "pug"
}))
// 注册 路由 信息
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000, () => {
console.log('监听3000端口')
})
{
// 创建管理员账户
const { db } = require('./schema/config.js')
const { userSchema } = require('./schema/user.js')
const encrypto = require('./util/crypt')
const User = db.model('users', userSchema)
// 初始化管理员账户 admin 密码 admin
User
.find({username: 'admin'})
.then(data => {
if (data.length === 0) {
// 没有则创建
new User({
username: 'admin',
password: encrypto('admin'),
role: '666',
commentNum: 0,
articleNum: 0
})
.save()
.then(data => {
console.log('管理源用户名admin密码admin')
})
.catch(err => {
console.log('管理员账号检查失败')
})
} else {
// 有则 console.log()
console.log('{admin: admin}')
}
})
}