summaryrefslogtreecommitdiff
path: root/server/index.js
blob: 1a0fdf17f48d89bac40832baeaed824f2ebaa3a6 (plain)
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
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}`)
})