forked from linguowei/myblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (49 loc) · 1.77 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
var express = require('express');
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var mime = require('mime');
var router = require('./server/router');
var compression = require('compression');
var session = require('express-session');
var FileStore = require('session-file-store')(session);
var app = express();
var resolve = file => path.resolve(__dirname, file);
app.use(compression());
app.use('/dist', express.static(resolve('./dist')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var identityKey = 'skey';
app.use(session({
name: identityKey,
secret: 'chyingp', // 用来对session id相关的cookie进行签名
store: new FileStore(), // 本地存储session(文本文件,也可以选择其他store,比如redis的)
saveUninitialized: false, // 是否自动保存未初始化的会话,建议false
resave: false, // 是否每次都重新保存会话,建议false
cookie: {
maxAge: 15 * 60 * 1000 // 有效期,单位是毫秒, 这里设置的是15分钟
}
}));
app.use(router)
// 后台管理页
app.get('/admin', function(req, res) {
var sess = req.session;
var loginUser = sess.loginUser;
var isLogined = !!loginUser;
if (isLogined){
console.log('已登录')
var html = fs.readFileSync(resolve('./' + 'admin.html'), 'utf-8');
}else{
console.log('未登录')
var html = fs.readFileSync(resolve('./' + 'login.html'), 'utf-8');
}
res.send(html)
});
// 博客首页
app.get('*', function(req, res) {
var html = fs.readFileSync(resolve('./' + 'index.html'), 'utf-8');
res.send(html)
});
app.listen(process.env.PORT || 7000, function() {
console.log("应用实例,访问地址为 localhost:7000")
});