-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
82 lines (60 loc) · 1.76 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
const Koa = require('koa');
const static = require('koa-static');
const router = require('./routers/router');
const views = require('koa-views');
const join = require('path').join;
const body = require('koa-body');
const session = require('koa-session');
const app = new Koa();
app.keys = ["这是签名"];
const CONFIG = {
key: "Sid",
maxAge: 36e5,
autoCommit: true,
overwrite: true,
httpOnly: true,
rolling: true,
renew: true
};
// 用户登录/注册 post
app.use(body());
app.use(session(CONFIG, app));
// 引入静态文件
app
.use(static(join(__dirname, 'public')))
.use(views(join(__dirname, 'views'), {extension: 'pug'}));
// 配置路由
app
.use(router.routes())
.use(router.allowedMethods())
.listen(3000, () => {
console.log("项目启动成功,监听在3000端");
});
// 创建管理员用户
const {db} = require('./Schema/config');
const UserSchema = require('./Schema/user');
const encrypt = require("./util/encrypt");
const User = db.model("users", UserSchema);
User
.find({username: "admin"})
.then(data => {
if(data.length === 0) {
// 管理员用户不存在-->创建
new User({
username: "admin",
password: encrypt("admin"),
role: 10,
articleNum: 0,
commentNum: 0
})
.save()
.then(data => {
console.log("创建成功==>管理员(Administrator)用户名-->admin, 密码-->admin");
})
.catch(err => {
console.log(err);
});
} else {
console.log("管理员(Administrator)用户名-->admin, 密码-->admin");
}
});