-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (47 loc) · 1.64 KB
/
Copy pathserver.js
File metadata and controls
56 lines (47 loc) · 1.64 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
50
51
52
53
54
55
56
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3000;
const TARGETS = {
'/api/deribit': 'https://www.deribit.com',
'/api/binance': 'https://eapi.binance.com',
'/api/bybit': 'https://api.bybit.com',
'/api/okx': 'https://www.okx.com',
};
for (const [prefix, target] of Object.entries(TARGETS)) {
app.use(prefix, async (req, res) => {
const url = target + req.url;
try {
const resp = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'application/json',
},
});
const contentType = resp.headers.get('content-type') || '';
const body = await resp.text();
// If exchange returns HTML instead of JSON (captcha/block page), return error JSON
if (contentType.includes('text/html') || body.trimStart().startsWith('<')) {
res.status(503).json({
error: `${prefix.replace('/api/', '')} returned non-JSON (possibly blocked)`,
code: resp.status,
});
return;
}
res.set('Content-Type', 'application/json');
res.set('Cache-Control', 'public, max-age=5');
res.send(body);
} catch (err) {
res.status(502).json({ error: err.message });
}
});
}
app.use(express.static(path.join(__dirname, 'dist')));
app.get('{*path}', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Options Scanner running on port ${PORT}`);
});