-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (73 loc) · 2.69 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch'; // Ensure node-fetch is installed
import dotenv from 'dotenv';
import { Client } from '@notionhq/client';
dotenv.config();
const app = express();
app.use(cors());
const API_KEY = process.env.VITE_NEWS_API_KEY;
const pageSize = 24;
const page = 1;
const country = 'in';
//~ server.js for avoiding CORS requests for fetching news based categories
app.get('/api/news', async (req, res) => {
const { category } = req.query;
try {
let url = `https://newsapi.org/v2/top-headlines?country=${country}&apiKey=${API_KEY}&page=${page}&pageSize=${pageSize}`;
if (category) {
url += `&category=${category}`;
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching news:', error);
res.status(500).json({ error: 'Failed to fetch news' });
}
});
//~ server.js for avoiding CORS requests for fetching news based on search term
app.get('/api/search', async (req, res) => {
const { query } = req.query;
try{
const url = `https://newsapi.org/v2/everything?q=${query}&apiKey=${API_KEY}&pageSize=${pageSize}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching search results:', error);
res.status(500).json({ error: 'Failed to fetch search results' });
}
})
//~ Fetching Notion data getting CORS issue for private data
const notion = new Client({ auth: process.env.VITE_NOTION_API_KEY });
app.get('/api/notion-page/:pageId', async (req, res) => {
try {
const response = await notion.pages.retrieve({ page_id: req.params.pageId });
res.json(response);
} catch (error) {
console.error('Error fetching Notion page:', error);
res.status(500).json({ error: 'Failed to fetch Notion page', details: error.message });
}
});
// Adding new endpoint in backend to fetch page content
app.get('/api/notion-blocks/:pageId', async (req, res) => {
try {
const blocks = await notion.blocks.children.list({
block_id: req.params.pageId,
page_size: 100,
});
res.json(blocks);
} catch (error) {
console.error('Error fetching Notion blocks:', error);
res.status(500).json({ error: 'Failed to fetch Notion blocks', details: error.message });
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));