-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (30 loc) · 1.11 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
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = 3000;
app.get('/', async (req, res) => {
try {
const { symbol } = req.query;
if (!symbol) {
return res.status(400).json({ error: 'symbol parameter is required. Ex: symbol=AAPL' });
}
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${symbol}?interval=1d&range=1d`;
const response = await axios.get(url);
const data = response.data;
res.json({
symbol,
currentPrice: data.chart.result[0].meta.regularMarketPrice,
exchangeName: data.chart.result[0].meta.exchangeName,
high: data.chart.result[0].indicators.quote[0].high,
low: data.chart.result[0].indicators.quote[0].low,
open: data.chart.result[0].indicators.quote[0].open,
close: data.chart.result[0].indicators.quote[0].close
});
} catch (error) {
console.error('Error fetching stock price:', error.message);
res.status(500).json({ error: 'Failed to fetch stock price' });
}
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});