-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (49 loc) · 1.5 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const axios = require('axios');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// Serve static files from the 'public' folder
app.use(express.static('public'));
const conservativeSources = [
'fox-news',
'breitbart-news',
'the-wall-street-journal',
'national-review',
'the-american-conservative',
// Add more conservative sources as identified
].join(',');
const liberalSources = [
'cnn',
'msnbc',
'the-huffington-post',
'the-washington-post',
'the-new-york-times',
// Add more liberal sources as identified
].join(',');
app.get('/news', async (req, res) => {
const { bias, ...otherParams } = req.query;
let sources = '';
if (bias === 'conservative') {
sources = conservativeSources;
} else if (bias === 'liberal') {
sources = liberalSources;
}
// Construct the API URL with all the query parameters
let apiUrl = `https://newsapi.org/v2/everything?apiKey=${process.env.NEWS_API_KEY}`;
apiUrl += sources ? `&sources=${sources}` : '';
// Add other query parameters
for (const param in otherParams) {
apiUrl += `&${param}=${encodeURIComponent(otherParams[param])}`;
}
try {
const response = await axios.get(apiUrl);
res.json(response.data);
} catch (error) {
console.error('Error fetching news:', error);
res.status(500).send('Error fetching news');
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});