-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (80 loc) · 2.58 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const http = require('http'),
express = require('express'),
wiki = require('wikijs').default,
generateDocx = require('generate-docx'),
logger = require('./logger'),
app = express(),
server = http.createServer(app),
PORT = process.env.PORT || 8000
app.set('view engine', 'jade')
app.use((req, res, next) => {
logger.info(req.method, req.url)
next()
})
app.use(express.static(__dirname + '/public'))
app.get('/', (req, res) => res.status(200).redirect('/home'))
app.get('/home', (req, res) => res.render('index'))
app.get('/search', (req, res) => {
wiki({ apiUrl: `http://${req.query.lng}.wikipedia.org/w/api.php` }).search(req.query.topic, 10)
.then(data => {
if (data.results[0])
res.render('list', {
array: data.results,
text: 'Выбирай что по вкусу, и через пару секунд мы сделаем тебе доклад ',
lng: req.query.lng
})
else
res.render('list', {
array: data.results,
text: 'Котя, по твоей теме мы ничего не нашли. Попробуй еще раз ',
lng: req.query.lng
})
})
.catch(e => logger.error(e))
})
app.get('/essay', (request, response) => {
let title = request.query.name,
options = {
template: {
filePath: './template.docx',
data: {
title,
description: 'Доклад студента КПИ',
body: ''
}
},
save: { filePath: './essay.docx' }
}
wiki({ apiUrl: `http://${request.query.lng}.wikipedia.org/w/api.php` }).page(title)
.then(page => page.content())
.then(res => {
if (request.query.lng == 'ru') {
let m = res.match('== См. также ==')
if (m) res = res.slice(0, m.index)
m = res.match('== Примечания ==')
if (m) res = res.slice(0, m.index)
} else {
let m = res.match('== See also ==')
if (m) res = res.slice(0, m.index)
m = res.match('== References ==')
if (m) res = res.slice(0, m.index)
}
res = res.replace(/={3,} /g, '== == ')
res = res.replace(/ ={3,}/g, ' == ==')
options.template.data.body = res + '\n\n created by fowi with <3'
generateDocx(options, (err, msg) => err ? logger.error(err) : response.download('./essay.docx'))
})
.catch(e => logger.error(e))
})
app.use((req, res, next) => {
throw new Error('not found')
})
app.use((err, req, res, next) => {
if (~err.message.indexOf('not found'))
res.status(404).send('not found 404')
else {
logger.error(err)
res.status(500).send('oops, something has broken :c\nerror 500')
}
})
server.listen(PORT, () => logger.info(`Server is running on port ${PORT}`))