-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (30 loc) · 1.05 KB
/
index.js
File metadata and controls
37 lines (30 loc) · 1.05 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
const express = require('express');
const fetch = require('node-fetch');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files
app.use(express.static('public'));
// API endpoint to fetch transaction data
app.get('/api/transactions/:address', async (req, res) => {
try {
const address = req.params.address;
const response = await fetch(`https://blockstream.info/api/address/${address}/txs`);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const transactions = await response.json();
res.json(transactions);
} catch (error) {
console.error('Error fetching transactions:', error);
res.status(500).json({ error: 'Failed to fetch transaction data' });
}
});
// Send the main page for any other route
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Open http://localhost:${PORT} in your browser`);
});