-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
114 lines (93 loc) · 2.85 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require("express");
const serverapp = express();
const cors = require("cors");
const path = require("path");
const axios = require("axios");
// const scholar = require("google-scholar");
const arxiv = require("arxiv-api");
const SerpApi = require('google-search-results-nodejs');
serverapp.use(
cors({
origin: "*",
})
);
serverapp.use(express.static(path.join(__dirname, "./public")));
// '/data' routi oz ichiga google scholar search engine malumotlarini oladi
serverapp.get("/data", async (req, res) => {
const query = req.query.q;
try {
const search = new SerpApi.GoogleSearch("Your_Private_Key");
const params = {
engine: "google_scholar",
q: query,
};
const callback = function(data) {
res.json(data["organic_results"]);
};
// Show result as JSON
search.json(params, callback);
} catch (error) {
console.error(error.message);
res.status(500).json({ error: "An error occurred" }); // Enhanced error response
}
});
serverapp.get('/arxiv', async (req, res) => {
const query = req.query.q;
try {
const arxivPapers = await arxiv.search({
searchQueryParams: [
{
include: [{ name: query}],
},
],
start: 0,
maxResults: 3
});
res.json(arxivPapers);
} catch (error) {
console.error(error); // Print the error for debugging
res.status(500).json({ error: 'An error occurred' }); // Send an error response
}
});
serverapp.get("/search", async function (req, res) {
const query = req.query.q;
const repos = [
{
url: `https://arxiv.org/search/?query=${query}&searchtype=all&source=header`,
title: "arXiv",
},
{
url: `https://www.biorxiv.org/search/${query}`,
title: "bioRxiv",
},
{
url: `https://www.medrxiv.org/search/${query}`,
title: "medRxiv",
},
{
url: `https://www.ncbi.nlm.nih.gov/pmc/?term=${query}`,
title: "NCBI PMC",
},
];
try {
const responseData = await Promise.all(
repos.map((repo) => axios.get(repo.url))
);
const dataArray = responseData.map((response) => {
let data = response.data;
// Replace the question mark character with the appropriate encoding
data = data.replace(/�/g, "è"); // Replace with the desired character
return data;
});
const htmlData = dataArray.join(""); // Combine all the processed data into a single HTML string
res.setHeader("Content-Type", "text/html"); // Set the content type as HTML
res.send(htmlData); // Send the HTML response
} catch (error) {
console.log("Error while fetching", error.message);
res.status(500).json({ error: "An error occurred" });
}
});
const port = 3000;
serverapp.listen(port, function () {
console.log(`Server: ${port}`);
});