fuzzyworld

dao-game / react / files / tools / build-and-serve.ts

Branch react · default branch · all branches

build-and-serve.ts

1.0 KB · history · blame

1import esbuild from 'esbuild';
2import http from 'http';
3import { serveOptions, buildOptions, hostPort, clearOutDir } from './settings'
4
5clearOutDir();
6
7esbuild.serve(serveOptions, buildOptions).then((result) => {
8 const { host, port } = result;
9 http
10 .createServer((req, res) => {
11 const options: http.RequestOptions = {
12 hostname: host,
13 port: port,
14 path: req.url,
15 method: req.method,
16 headers: req.headers,
17 };
18 const proxyReq = http.request(options, (proxyRes) => {
19 if (proxyRes.statusCode === 404) {
20 res.writeHead(404, { 'Content-Type': 'text/html' });
21 res.end('<h1>A custom 404 page</h1>');
22 return;
23 }
24
25 res.writeHead(proxyRes.statusCode!, proxyRes.headers);
26 proxyRes.pipe(res, { end: true });
27 });
28
29 req.pipe(proxyReq, { end: true });
30 })
31 .listen(hostPort, () => {
32 const url = `http://localhost:${hostPort}/`;
33 // childProcess.exec(`start ${url}`);
34 console.log(`Server serving at ${url}`);
35 });
36});