-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (35 loc) · 1.4 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
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const path = 'cache';
const app = express();
app.get('/api/search', (req, res) => {
const query = (req.query.query).trim();
let result = {};
const searchUrl = `https://en.wikipedia.org/w/api.php?action=parse&format=json§ion=0&page=${query}`;
fs.readdir(`${path}/${query}`, (err, items) => {
if (err) {
fs.mkdir(`${path}/${query}`, () => {
axios.get(searchUrl)
.then((response) => {
const responseJSON = response.data;
fs.writeFileSync(`${path}/${query}/${query}.txt`, JSON.stringify(responseJSON), function (err) {
console.log('File is created successfully.');
});
return res.status(200).json({ source: 'Wikipedia API', ...responseJSON, });
},
(error) => {
return res.json(err);
})
});
} else {
items.forEach((i) => {
result = Object.assign(result, JSON.parse(fs.readFileSync(`${path}/${query}/${i}`, "utf8")));
})
return res.status(200).json({ source: 'Cache API', ...result, });
}
})
});
app.listen(3000, () => {
console.log('Server listening on port: ', 3000);
});