-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(express): Refactor static server scripts
- Loading branch information
1 parent
ec071d2
commit e362275
Showing
5 changed files
with
72 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-env browser */ | ||
|
||
const onAction = () => { | ||
fetch("/status") | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
const status = data.idle ? "Idling" : "Running"; | ||
const percentage = data.total ? (100 * data.done) / data.total : 0; | ||
const text = `${status} ${percentage.toFixed(2)} (${data.done} of ${ | ||
data.total | ||
})`; | ||
const el = document.getElementById("statusEl"); | ||
el.textContent = text; | ||
|
||
if (!data.idle || data.done !== data.total) { | ||
setTimeout(() => onAction(), 500); | ||
} | ||
}); | ||
}; | ||
|
||
const onFileSelect = () => { | ||
const el = document.getElementById("fileEl"); | ||
el.setAttribute("disabled", "disabled"); | ||
|
||
fetch("/import", { | ||
method: "POST", | ||
body: el.files[0], | ||
}) | ||
.then(() => onAction()) | ||
.finally(() => el.removeAttribute("disabled")); | ||
}; | ||
|
||
// Attaching event handlers | ||
document | ||
.querySelector("#fileEl") | ||
.addEventListener("onchange", () => onFileSelect()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { fileURLToPath } from "node:url"; | ||
import { resolve as resolvePath } from "node:path"; | ||
import express from "express"; | ||
|
||
export const initStaticServer = ( | ||
script: "import" | "chart", | ||
): express.Express => { | ||
const currentDir = fileURLToPath(new URL(".", import.meta.url)); | ||
const files = { | ||
html: resolvePath(currentDir, `../${script}/index.html`), | ||
js: resolvePath(currentDir, `../${script}/index.js`), | ||
}; | ||
|
||
const app = express(); | ||
app.use(express.json({ limit: "102400kb" })); | ||
|
||
app.get("/", (req: express.Request, res: express.Response) => { | ||
res.status(200).sendFile(files.html); | ||
}); | ||
|
||
app.get("/index.js", (req: express.Request, res: express.Response) => { | ||
res.status(200).sendFile(files.js); | ||
}); | ||
|
||
app.get("/favicon.ico", (_req, res: express.Response<string>) => { | ||
res.status(204).send(""); | ||
}); | ||
|
||
return app; | ||
}; |