1
0
Fork 0
taleweave-ai/client/serve.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-05-04 20:36:55 +00:00
import { mustDefault } from '@apextoaster/js-utils';
import { readFile } from 'fs';
import { createServer } from 'http';
import { join } from 'path';
const { env } = process;
const host = mustDefault(env.ONNX_WEB_DEV_HOST, '127.0.0.1');
const port = mustDefault(env.ONNX_WEB_DEV_PORT, '8000');
const root = process.cwd();
const portNum = parseInt(port, 10);
const contentTypes = [
[/^.*\.html$/, 'text/html'],
[/^.*\.js$/, 'application/javascript'],
[/^.*\.json$/, 'text/json'],
];
function getContentType(path) {
for (const [regex, type] of contentTypes) {
if (regex.test(path)) {
return type;
}
}
return 'unknown';
}
const server = createServer((req, res) => {
const path = join(root, 'out', req.url || 'index.html');
readFile(path, function (err, data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200, {
'Content-Type': getContentType(path),
});
res.end(data);
});
});
server.listen(portNum, host, () => {
console.log(`Dev server running at http://${host}:${port}/index.html`);
});