-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (33 loc) · 1.16 KB
/
server.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
const express = require('express');
var cors = require('cors');
const mwn = require('mwn');
const app = express();
app.use(express.json());
app.use(express.static('static'));
app.use(cors());
const port = parseInt(process.env.PORT, 10);
const bot = new mwn({ apiUrl: 'https://en.wikipedia.org/w/api.php' });
const TextExtractor = require('./TextExtractor')(bot);
var handleRequest = (req, res) => {
var params = Object.assign({}, req.query, req.body);
if (params.article) {
bot.read(params.article).then(data => {
var text = data.revisions[0].content;
res.send(TextExtractor.getExtract(text, params.charLimit, params.hardUpperLimit));
});
} else if (params.wikitext) {
res.send(TextExtractor.getExtract(params.wikitext, params.charLimit, params.hardUpperLimit));
}
};
bot.getSiteInfo().then(() => {
app.get('/', (req, res) => {
res.sendFile(__dirname + '/static/index.html');
});
app.get('/summary', (req, res) => {
handleRequest(req, res);
});
app.post('/summary', (req, res) => {
handleRequest(req, res);
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
});