-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (74 loc) · 2.21 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
var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var session = require('express-session')
var redisStore = require('connect-redis')(session)
var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
var shorturlRouter = require('./routes/shorturl')
var jumpurlRouter = require('./routes/jumpurl')
var app = express()
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({extended: false}))
app.use(cookieParser('yog'))
app.use(session({
//name: 'session_id', // 这里是cookie的name,默认是connect.sid
secret: 'yog', // 建议使用 128 个字符的随机字符串
resave: true,
saveUninitialized: false,
cookie: {
// 'signed': true,
'maxAge': 1000 * 600,
'httpOnly': false,
//'username':'default'
},
store: new redisStore({
host: '127.0.0.1',
port: '6379',
db: 0,
pass: '',
})
}))
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.engine('.html', require('ejs').__express)
app.set('view engine', 'html')
//鉴权中间件
let auth = (req, res, next) => {
let authArr = ['/myshortlink', '/users']
if (authArr.indexOf(req.path) > -1 || req.path.indexOf('/users') > -1) {
let cookieSessionId = req.signedCookies['connect.sid']
let sessionId = req.session.id
if (cookieSessionId === void 0 || sessionId === void 0 || cookieSessionId !== sessionId) {
res.clearCookie('user', {
'maxAge': 1000 * 600, // 有效时长(毫秒)
'signed': false // 默认为false,表示是否签名(Boolean)
})
req.session.destroy() // 清空所有session
res.redirect('/login')
} else {
next()
}
// let cookieUser = req.cookies.user
// let sessionUser = req.session.user
} else {
next()
}
}
app.use(auth)
app.use('/', indexRouter)
app.use('/s', jumpurlRouter)
app.use('/users', usersRouter)
app.use('/su', shorturlRouter)
app.get('*', (req, res) => {
res.render('404', {
title: '404 Not Found',
})
})
app.use((err,req,res,next)=>{
console.error(err.stack)
res.status(500).send('Something broke')
})
module.exports = app