index.js
1.69 KB
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
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const sessions = require("client-sessions");
const querystring = require('querystring');
const render = require('./render');
const config = require('../config/common');
global.config = config;
global.utilPath = path.join(__dirname, '../utils');
global.auth = (req, res, next) => {
if (!req.session.user) {
if (req.xhr) {
res.json({
code: 401,
message: '抱歉,您暂未登录!'
});
return false;
}
res.redirect(`/login.html?${querystring.stringify({ refer: req.originalUrl})}`);
return false;
}
next && next();
return true;
};
exports.createServer = async() => {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../dist')));
app.use(sessions({
requestKey: 'session',
cookieName: 'ufo_stats_session',
secret: 'yoho_ufo_stats_9646',
duration: 24 * 60 * 60 * 1000,
activeDuration: 1000 * 60 * 5
}));
// dispatcher
try {
require('./dispatch')(app);
app.all('*', render);
// 后置中间件
app.use((err, req, res, next) => {
console.log(err)
return res.status(500).end();
});
} catch (err) {
console.error(err);
}
// listener
app.listen(config.port, function () {
console.log('Project server listening on port ' + config.port);
});
};