ka1kuk commited on
Commit
e5d9e2e
1 Parent(s): f3837b0

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +21 -0
app.js CHANGED
@@ -36,6 +36,27 @@ app.get('/api/quote/:symbol', async (req, res) => {
36
  }
37
  });
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  app.get('/api/chart/:symbol', async (req, res) => {
40
  const symbol = req.params.symbol;
41
  const interval = req.query.interval || '1d'; // Default to 1 day
 
36
  }
37
  });
38
 
39
+ app.get('/api/history/:symbol', async (req, res) => {
40
+ const symbol = req.params.symbol;
41
+ const interval = req.query.interval || '1d'; // Default to 1 day
42
+ const range = req.query.range || '1y'; // Default to 1 year
43
+ try {
44
+ const url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=${interval}&range=${range}`;
45
+ const response = await fetch(url);
46
+ const data = await response.json();
47
+
48
+ if (!data.chart.result) {
49
+ return res.status(404).json({ message: `No historical data found for ${symbol}` });
50
+ }
51
+
52
+ res.json(data);
53
+
54
+ } catch (error) {
55
+ console.error(`Error fetching historical data for ${symbol}:`, error);
56
+ res.status(500).json({ message: 'Error fetching historical data.', error: error.message });
57
+ }
58
+ })
59
+
60
  app.get('/api/chart/:symbol', async (req, res) => {
61
  const symbol = req.params.symbol;
62
  const interval = req.query.interval || '1d'; // Default to 1 day