1
0
Fork 0
onnx-web/gui/serve.js

29 lines
748 B
JavaScript
Raw Normal View History

import { mustDefault } from '@apextoaster/js-utils';
2023-01-05 03:55:25 +00:00
import { readFile } from 'fs';
import { createServer } from 'http';
import { join } from 'path';
const { env } = process;
const host = mustDefault(env.ONNX_WEB_DEV_HOST, '0.0.0.0');
const port = mustDefault(env.ONNX_WEB_DEV_PORT, '3000');
2023-01-05 03:55:25 +00:00
const root = process.cwd();
const portNum = parseInt(port, 10);
2023-01-05 03:55:25 +00:00
const server = createServer((req, res) => {
readFile(join(root, 'out', req.url || 'index.html'), function (err, data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
});
server.listen(portNum, host, () => {
console.log(`Dev server running at http://${host}:${port}/index.html`);
2023-01-05 03:55:25 +00:00
});