-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (73 loc) · 2.3 KB
/
index.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
/*
*入口模块
*/
const express = require('express')
const session = require('cookie-session')
const multer = require('multer')
const path = require('path')
const fs = require('fs')
//创建主应用
const app =express()
// 上传配置
const upload = multer({
dest: './static/upload', // 上传文件的存储目录
limits: {
fileSize: 1024 * 1024 * 2 // 单个文件大小限制在2M以内
}
})
//模板引擎的设置
app.set('view engine','html')
app.set('views',`${__dirname}/views`)
app.engine('html',require('ejs').renderFile)
//静态资源配置
app.use(express.static('static'))
//POST请求处理
app.use(express.urlencoded({ extended:true }))
//session配置
app.use(session({
keys:['secret'],
maxAge:60*1000*30
}))
//调用首页子应用
app.use(/\/(index)?/,require('./router/index'))
//调用文章子应用
app.use('/article',require('./router/article'))
//调用搜索子应用
app.use('/search',require('./router/search'))
//调用登录子应用
app.use('/login',require('./router/login'))
//调用注册子应用
app.use('/reg',require('./router/reg'))
//进入后台的权限验证(('/admin/?*'):给所有的后台页面加上权限验证
app.use('/admin/?*',require('./middleware/auth').alloToAdmin)
// 上传操作
app.post('/admin/*', upload.single('upload'), (req, res, next) => {
// 上传成功后的文件对象
let { file } = req
if (file) {
// file.originalname ==> 文件的原名称
let extname = path.extname(file.originalname)
// file.path ==> 上传后的文件路径
fs.renameSync(file.path, file.path + extname)
// file.filename ==> 上传后的文件名
req.uploadUrl = '/upload/' + file.filename + extname
}
next()
})
//调用后台首页
app.use(/\/admin\/(index)?/,require('./router/admin/index'))
//调用后台文章管理
app.use('/admin/article',require('./router/admin/article'))
//调用后台类目管理
app.use('/admin/category',require('./router/admin/category'))
//调用后台日志管理
app.use('/admin/log',require('./router/admin/log'))
//调用后台账户管理
app.use('/admin/account',require('./router/admin/account'))
//退出操作
app.get('/user/logout',(req,res)=>{
req.session.user =null
res.render('login',{msg:'退出成功'})
})
//监听服务
app.listen(3000)