-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (47 loc) · 1.13 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
const express = require("express");
const cors = require("cors");
const wiki = require("wikipedia");
const app = express();
const port = 3000;
app.use(cors({
origin:"*"
}));
// Searching for any topic
app.get("/search", async (req, res) => {
const query = req.query.topic;
const response = await wiki.search(query);
let results = {};
if (response.results.length > 0) {
results = await response.results.map((element) => element.title);
} else {
results = {
message: "No result found.",
code: 404,
};
}
res.send(results);
});
// Getting short summary for a topic
app.get("/summary", async (req, res) => {
const query = req.query.topic;
let result = {};
try {
const response = await wiki.summary(query);
result = {
title: response.title,
"image-url": response.originalimage.source,
description: response.description,
"short-summary": response.extract,
};
} catch (error) {
console.log(error);
result = {
message: "No result found.",
code: 404,
};
}
res.send(result);
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});