summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
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}`)
+})