summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorDaniel Andreas Wang <danielaw@tilde.club>2026-03-12 19:01:51 +0100
committerDaniel Andreas Wang <danielaw@tilde.club>2026-03-12 19:01:51 +0100
commit2c46000979b2c9906c5066821c43ddf0ae7ff090 (patch)
tree57c685eb8913fdebf2ba9f4de441f0263e33be27 /server
parentad9c2429def17cd73d0cc22837ef278abcd4f64a (diff)
set up news fetching
Diffstat (limited to 'server')
-rw-r--r--server/index.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/server/index.js b/server/index.js
new file mode 100644
index 0000000..1a0fdf1
--- /dev/null
+++ b/server/index.js
@@ -0,0 +1,29 @@
+import 'dotenv/config'
+import express from 'express'
+
+const app = express()
+const PORT = 3000
+const API_KEY = process.env.NEWS_API_KEY
+const BASE_URL = 'https://newsapi.org/v2'
+
+async function proxyNewsApi(path, query, res) {
+ if (!API_KEY) {
+ return res.status(500).json({ error: 'NEWS_API_KEY is not set' })
+ }
+ const params = new URLSearchParams({ ...query, apiKey: API_KEY })
+ const response = await fetch(`${BASE_URL}${path}?${params}`)
+ const data = await response.json()
+ res.status(response.status).json(data)
+}
+
+app.get('/api/top-headlines', async (req, res) => {
+ await proxyNewsApi('/top-headlines', req.query, res)
+})
+
+app.get('/api/everything', async (req, res) => {
+ await proxyNewsApi('/everything', req.query, res)
+})
+
+app.listen(PORT, () => {
+ console.log(`Server running on http://localhost:${PORT}`)
+})