-
Notifications
You must be signed in to change notification settings - Fork 8
/
router.js
76 lines (63 loc) · 1.88 KB
/
router.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
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);
const mysql = require('mysql');
const app = require('express')();
const dbconf = require('./config');
// 数据库连接池
const DB = 'wooyun',
BUGTB = 'bugs',
DROPTB = 'drops';
const pool = mysql.createPool({
user: dbconf.dbuser,
password: dbconf.dbpass,
});
const esc = pool.escape.bind(pool);
var type = '',
content = '';
app.get('/', (req, res) => {
res.render('search',{
page: 'index',
});
});
app.get('/result', (req, res) => {
type = req.query.type;
content = `%${req.query.content}%`;
res.render('result',{
page: 'result',
content: req.query.content,
});
});
app.get('/check', (req, res) => {
let page = req.query.page*20 || 0;
let client, q, result, count;
if (!type) {
res.json({
code: 500,
error: '未选择类型'
})
return;
}
return co(function*() {
client = yield pool.getConnectionAsync();
q = client.queryAsync.bind(client);
yield q(`use ${DB}`);
if (type === 'default') {
count = yield q(`select count(*) from ${BUGTB}`);
result = yield q(`select * from ${BUGTB} order by date desc limit ${page},20`);
} else {
count = yield q(`select count(*) from ${BUGTB} where ${type} like ${esc(content)}`);
result = yield q(`select * from ${BUGTB} where ${type} like ${esc(content)} order by date desc limit ${page},20`);
}
res.render('table', {
page: 'result',
layout: false,
result: result,
count: count[0]['count(*)'],
});
client.release();
})();
});
module.exports = app;