-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
60 lines (51 loc) · 1.74 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
// Import necessary modules
import express from 'express';
import fetch from 'node-fetch';
import { fileURLToPath } from 'url';
import path from 'path';
import { dirname } from 'path';
import cors from 'cors';
import dotenv from 'dotenv';
import { defaultMiddleware } from '@nlbridge/express';
dotenv.config();
const app = express();
const port = 3300;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
// Routes
app.get('/data', async (req, res) => {
try {
const dateThreeDaysAgo = getDateThreeDaysAgo();
const response = await fetch(`https://newsapi.org/v2/everything?q=finance&excludeDomains=yahoo.com&language=en&from=${dateThreeDaysAgo}&pageSize=10&sortBy=relevancy&apiKey=${process.env.NEWS_API_KEY}`);
const data = await response.json();
console.log(data);
res.json(data);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Chat API endpoint using default middleware
app.post('/chat-api',
defaultMiddleware('openai', {
apiKey: process.env.OPENAI_API_KEY,
chatModel: 'gpt-3.5-turbo',
})
);
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// Functions
function getDateThreeDaysAgo() {
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
const year = threeDaysAgo.getFullYear();
const month = String(threeDaysAgo.getMonth() + 1).padStart(2, '0');
const date = String(threeDaysAgo.getDate()).padStart(2, '0');
return `${year}-${month}-${date}`;
}