-
Notifications
You must be signed in to change notification settings - Fork 214
/
index.js
53 lines (45 loc) · 1.21 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
const express = require('express');
const app = express();
const request = require('request');
const wikip = require('wiki-infobox-parser');
//ejs
app.set("view engine", 'ejs');
//routes
app.get('/', (req,res) =>{
res.render('index');
});
app.get('/index', (req,response) =>{
let url = "https://en.wikipedia.org/w/api.php"
let params = {
action: "opensearch",
search: req.query.person,
limit: "1",
namespace: "0",
format: "json"
}
url = url + "?"
Object.keys(params).forEach( (key) => {
url += '&' + key + '=' + params[key];
});
//get wikip search string
request(url,(err,res, body) =>{
if(err) {
response.redirect('404');
}
result = JSON.parse(body);
x = result[3][0];
x = x.substring(30, x.length);
//get wikip json
wikip(x , (err, final) => {
if (err){
response.redirect('404');
}
else{
const answers = final;
response.send(answers);
}
});
});
});
//port
app.listen(3000, console.log("Listening at port 3000..."))