UNPKG

ts-to-html

Version:

TS and SASS compiler for a HTML with live preview

33 lines (32 loc) 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_fs_1 = require("node:fs"); const node_http_1 = require("node:http"); const node_path_1 = require("node:path"); const PORT = process.env.PORT || "1002"; // config env const server = (0, node_http_1.createServer)((req, res) => { const endRes = (code, message) => { res.statusCode = code; message && (res.statusMessage = JSON.stringify({ message })); return res.end(); }; if (String(req.method).toUpperCase() === "GET") { try { const url = req.url || "/"; const path = (0, node_path_1.resolve)(process.cwd(), "public", url.length <= 1 ? "index.html" : "./" + url); if ((0, node_fs_1.existsSync)(path)) { res.statusCode = 200; return res.end((0, node_fs_1.readFileSync)(path).toString().replace(/%PUBLIC_URL%/g, process.env.PUBLIC_URL || "")); } else return endRes(404, "Not found"); } catch (error) { return endRes(500, "An internal error has occured."); } } else endRes(401, "Unauthorized"); }); exports.default = (callback) => server.listen(PORT, () => callback(PORT));