-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ts
55 lines (44 loc) · 1.42 KB
/
server.ts
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
import { file, serve } from 'bun';
const basePath = './dist/ngclient/browser';
serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
console.log(url.pathname);
let filePath = `${basePath}${url.pathname}`;
let lang = url.pathname.split('/')[1];
console.log(lang);
// if (url.pathname.startsWith('/api')) {
// return Response.redirect(`localhost:8200${url.pathname}`);
// }
if (url.pathname.startsWith('/api/')) {
// Reverse proxy requests to /api to localhost:8200
url.hostname = 'localhost';
url.port = '8200';
const proxyRequest = new Request(url, {
method: req.method,
headers: req.headers,
body: req.body,
});
// proxyRequest.headers.set("Host", host);
return await fetch(proxyRequest, { redirect: 'manual' });
// return fetch(
// );
} else if (url.pathname.startsWith('/en-US/') || url.pathname.startsWith('/fr-FR/')) {
// ...
} else {
return Response.redirect('/en-US/');
}
try {
if (url.pathname.split('/').at(-1)?.includes('.')) {
return new Response(file(filePath));
} else {
return new Response(file(`${basePath}/${lang}/index.html`));
}
} catch (error) {
// If the file is not found, return a 404
return new Response('Not Found', { status: 404 });
}
},
});
console.log('Server running on http://localhost:3000');