-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
index.js
61 lines (60 loc) · 1.83 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import express from 'express';
import http from 'node:http';
import path from 'node:path';
import cors from 'cors';
import { createBareServer } from "@tomphttp/bare-server-node";
import fetch from 'node-fetch'; // Make sure to install 'node-fetch' package
const app = express();
const server = http.createServer(app);
const __dirname = process.cwd();
const PORT = 8080;
const bareServer = createBareServer('/outerspace/');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.static(path.join(__dirname, 'static')));
const routes = [
{ path: '/', file: 'index.html' },
{ path: '/news', file: 'apps.html' },
{ path: '/events', file: 'games.html' },
{ path: '/diagnostic', file: 'settings.html' },
{ path: '/local-news', file: 'tabs.html' },
{ path: '/image-galleries', file: 'go.html' },
];
app.get('/edu/*', cors({ origin: false }), async (req, res, next) => {
try {
const reqTarget = `https://raw.githubusercontent.com/InterstellarNetwork/Interstellar-Assets/main/${req.params[0]}`;
const asset = await fetch(reqTarget);
if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
} else {
next();
}
} catch (error) {
console.error('Error fetching:', error);
next(error);
}
});
routes.forEach((route) => {
app.get(route.path, (req, res) => {
res.sendFile(path.join(__dirname, 'static', route.file));
});
});
server.on('request', (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen(PORT, () => {
console.log(`Running at http://localhost:${PORT}`);
});