-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (41 loc) · 1.58 KB
/
app.js
File metadata and controls
49 lines (41 loc) · 1.58 KB
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
const express = require('express')
const path = require('path')
const stocks = require('./stocks')
const app = express()
app.use(express.static(path.join(__dirname, 'static')))
app.get('/stocks', async (req, res) => {
const stockSymbols = await stocks.getStocks()
res.send({ stockSymbols })
})
app.get('/stocks/:symbol', async (req, res) => {
const { params: { symbol } } = req
const data = await stocks.getStockPoints(symbol, new Date())
res.send(data)
})
app.listen(3000, () => console.log('Server is running!'))
const express = require('express');
const path = require('path');
const stocks = require('../stocks'); // Assuming stocks-related files are in the parent directory
const app = express();
app.use(express.static(path.join(__dirname, '../static')));
app.get('/stocks', async (req, res) => {
try {
const stockSymbols = await stocks.getStocks();
res.send({ stockSymbols });
} catch (error) {
console.error('Error fetching stock list:', error);
res.status(500).send('Internal Server Error: Unable to fetch stocks');
}
});
app.get('/stocks/:symbol', async (req, res) => {
try {
const { params: { symbol } } = req;
const data = await stocks.getStockPoints(symbol, new Date());
res.send(data);
} catch (error) {
console.error(`Error fetching data for ${req.params.symbol}:`, error);
res.status(500).send(`Internal Server Error: Unable to fetch data for ${req.params.symbol}`);
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server is running on port ${PORT}!`));