-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.54 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
const express = require('express');
const expressStatic = require('express-static');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const multer = require('multer');
const consolidate = require('consolidate');
const { getNewsList, getNewsDetail } = require('./libs/spider');
const tools = require('./libs/comment');
// express实例
const server = express();
// 防session劫持keys数组
const keys = [];
for (let i = 0; i < 100000; i++) {
keys.push('key_' + Math.random());
}
// 引擎配置
server.set('view engine', 'html');
server.set('views', __dirname + '/view');
server.engine('html', consolidate.ejs);
// post解析
server.use(bodyParser.urlencoded({ extended: false }));
server.use(multer({ dest: __dirname + '/dest' }).any());
// cookie session
server.use(cookieParser('dwhdwidhwidqizn'));
server.use(cookieSession({
name: 'sess',
keys,
maxAge: 20 * 60 * 1000
}));
// 用户请求
// 首页
server.get('/', (req, res) => {
getNewsList((banner, article) => {
res.render('index.ejs', {
banner,
article
});
});
});
// 详情
server.get('/article', (req, res) => {
if (req.query.id) {
getNewsDetail(req.query.id, (article) => {
res.render('page.ejs', {
article_data: article
});
});
} else {
res.status(404).send('文章找不到').end();
}
});
// 静态文件
server.use(expressStatic(__dirname + '/dist'));
module.exports = server;