forked from mrsproutt/3kh0-CDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (27 loc) · 1.08 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
import express from 'express';
import fetch from 'node-fetch';
const app = express();
const repository = '3kh0/3kh0-Assets/main';
app.all('/', async (req, res) => {
const file = await fetch('https://raw.githubusercontent.com/3kh0/3kh0.github.io/main/assets/games.json');
const games = await file.json();
res.json(games);
})
app.all('*', async (req, res) => {
try {
const file = await fetch(`https://raw.githubusercontent.com/${repository}${req.originalUrl}`);
const data = new Buffer.from(await file.arrayBuffer());
if (file.headers.get('content-type').split(';')[0] == 'text/plain' && req.path.endsWith('.html') || req.path.endsWith('.htm')) {
res.writeHead(file.status, { 'Content-Type': 'text/html' })
} else {
res.writeHead(file.status, { 'Content-Type': file.headers.get('content-type').split(';')[0] })
}
res.end(data);
} catch (e) {
res.sendStatus(404);
throw new Error(e);
}
})
app.listen(9000, () => {
console.log('Your 3kh0.github.io CDN server is running on port 9000')
})