-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 loc) · 1.78 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
54
55
56
const express = require('express');
const { parseStringPromise } = require('xml2js');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
const searchPapers = async (query, maxResults = 10) => {
const base_url = 'https://export.arxiv.org/api/query';
const apiUrl = `${base_url}?search_query=${encodeURIComponent(query)}&max_results=${maxResults}`;
const response = await fetch(apiUrl);
const dataXml = await response.text();
const data = await parseStringPromise(dataXml);
const papers = [];
const entries = data.feed.entry ?? [];
for (const entry of entries) {
const paper = {
title: entry.title[0],
authors: entry.author.map((author) => author.name[0]),
summary: entry.summary[0],
link: entry.link.find((link) => link.$.title === 'pdf').$.href,
};
papers.push(paper);
}
return papers;
};
// Serve the openapi.yaml and ai-plugin.json static files from the root directory
app.use('/openapi.yml', express.static('openapi.yml'));
app.use('/legal_info.html', express.static('legal_info.html'));
app.use('/.well-known/ai-plugin.json', express.static('ai-plugin.json'));
app.get('/search', async (req, res) => {
const query = req.query.query;
const maxResults = parseInt(req.query.maxResults, 10) || 10;
if (query) {
try {
const papers = await searchPapers(query, maxResults);
res.json(papers);
} catch (error) {
res.status(500).json({ error: 'An error occurred while searching for papers.' });
}
} else {
res.status(400).json({ error: 'Query parameter is required.' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});