|
1 | | -import express from 'express'; |
2 | | -import http from 'node:http'; |
3 | | -import { createBareServer } from "@tomphttp/bare-server-node"; |
4 | | -import path from 'node:path'; |
5 | | -import cors from 'cors'; |
6 | | - |
7 | | -const __dirname = process.cwd(); |
8 | | -const server = http.createServer(); |
9 | | -const app = express(server); |
10 | | -const bareServer = createBareServer('/v/'); |
11 | | -const PORT = 8080; |
12 | | - |
13 | | -app.use(express.json()); |
14 | | -app.use(express.urlencoded({ extended: true })); |
15 | | -app.use(cors()); |
16 | | -app.use(express.static(path.join(__dirname, 'static'))); |
17 | | - |
18 | | -const routes = [ |
19 | | - { path: '/', file: 'index.html' }, |
20 | | - { path: '/~', file: 'apps.html' }, |
21 | | - { path: '/-', file: 'games.html' }, |
22 | | - { path: '/!', file: 'settings.html' }, |
23 | | - { path: '/0', file: 'tabs.html' }, |
24 | | - { path: '/&', file: 'go.html' }, |
25 | | - { path: '/w', file: 'edu.html' }, |
26 | | -]; |
27 | | - |
28 | | -app.get('/y/*', cors({ origin: false }), async (req, res, next) => { |
29 | | - try { |
30 | | - const reqTarget = `https://raw.githubusercontent.com/ypxa/y/main/${req.params[0]}`; |
31 | | - const asset = await fetch(reqTarget); |
32 | | - |
33 | | - if (asset.ok) { |
34 | | - const data = await asset.arrayBuffer(); |
35 | | - res.end(Buffer.from(data)); |
36 | | - } else { |
37 | | - next(); |
38 | | - } |
39 | | - } catch (error) { |
40 | | - console.error('Error fetching:', error); |
41 | | - next(error); |
42 | | - } |
43 | | -}); |
44 | | - |
45 | | -routes.forEach((route) => { |
46 | | - app.get(route.path, (req, res) => { |
47 | | - res.sendFile(path.join(__dirname, 'static', route.file)); |
48 | | - }); |
49 | | -}); |
50 | | - |
51 | | -server.on('request', (req, res) => { |
52 | | - if (bareServer.shouldRoute(req)) { |
53 | | - bareServer.routeRequest(req, res); |
54 | | - } else { |
55 | | - app(req, res); |
56 | | - } |
57 | | -}); |
58 | | - |
59 | | -server.on('upgrade', (req, socket, head) => { |
60 | | - if (bareServer.shouldRoute(req)) { |
61 | | - bareServer.routeUpgrade(req, socket, head); |
62 | | - } else { |
63 | | - socket.end(); |
64 | | - } |
65 | | -}); |
66 | | - |
67 | | -server.on('listening', () => { |
68 | | - console.log(`Running at http://localhost:${PORT}`); |
69 | | -}); |
70 | | - |
71 | | -server.listen({ |
72 | | - port: PORT, |
73 | | -}); |
| 1 | +import express from 'express' |
| 2 | +import http from 'node:http' |
| 3 | +import { createBareServer } from '@tomphttp/bare-server-node' |
| 4 | +import path from 'node:path' |
| 5 | +import cors from 'cors' |
| 6 | + |
| 7 | +const __dirname = process.cwd() |
| 8 | +const server = http.createServer() |
| 9 | +const app = express(server) |
| 10 | +const bareServer = createBareServer('/v/') |
| 11 | +const PORT = 8080 |
| 12 | + |
| 13 | +app.use(express.json()) |
| 14 | +app.use(express.urlencoded({ extended: true })) |
| 15 | +app.use(cors()) |
| 16 | +app.use(express.static(path.join(__dirname, 'static'))) |
| 17 | + |
| 18 | +const routes = [ |
| 19 | + { path: '/', file: 'index.html' }, |
| 20 | + { path: '/~', file: 'apps.html' }, |
| 21 | + { path: '/-', file: 'games.html' }, |
| 22 | + { path: '/!', file: 'settings.html' }, |
| 23 | + { path: '/0', file: 'tabs.html' }, |
| 24 | + { path: '/&', file: 'go.html' }, |
| 25 | + { path: '/w', file: 'edu.html' }, |
| 26 | +] |
| 27 | + |
| 28 | +app.get('/y/*', cors({ origin: false }), async (req, res, next) => { |
| 29 | + try { |
| 30 | + const reqTarget = `https://raw.githubusercontent.com/ypxa/y/main/${req.params[0]}` |
| 31 | + const asset = await fetch(reqTarget) |
| 32 | + |
| 33 | + if (asset.ok) { |
| 34 | + const data = await asset.arrayBuffer() |
| 35 | + res.end(Buffer.from(data)) |
| 36 | + } else { |
| 37 | + next() |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + console.error('Error fetching:', error) |
| 41 | + next(error) |
| 42 | + } |
| 43 | +}) |
| 44 | + |
| 45 | +routes.forEach((route) => { |
| 46 | + app.get(route.path, (req, res) => { |
| 47 | + res.sendFile(path.join(__dirname, 'static', route.file)) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +server.on('request', (req, res) => { |
| 52 | + if (bareServer.shouldRoute(req)) { |
| 53 | + bareServer.routeRequest(req, res) |
| 54 | + } else { |
| 55 | + app(req, res) |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +server.on('upgrade', (req, socket, head) => { |
| 60 | + if (bareServer.shouldRoute(req)) { |
| 61 | + bareServer.routeUpgrade(req, socket, head) |
| 62 | + } else { |
| 63 | + socket.end() |
| 64 | + } |
| 65 | +}) |
| 66 | + |
| 67 | +server.on('listening', () => { |
| 68 | + console.log(`Running at http://localhost:${PORT}`) |
| 69 | +}) |
| 70 | + |
| 71 | +server.listen({ |
| 72 | + port: PORT, |
| 73 | +}) |
0 commit comments