forked from HellSquirrel/doka-demo-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
55 lines (41 loc) · 1.19 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import express from "express";
import { createServer as createViteServer } from "vite";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function createServer() {
const app = express();
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "custom",
});
app.use(vite.middlewares);
app.use("/unicorns", (req, res) => {
if (fs.existsSync(path.resolve(__dirname, "unicorns"))) {
fs.unlinkSync(path.resolve(__dirname, "unicorns"));
}
const stream = fs.createWriteStream(
path.resolve(__dirname, "unicorns")
);
req.pipe(stream);
req.on("data", console.log);
req.on("end", () => {
res.send("thanks!");
stream.close();
});
});
app.use("*", async (_, res) => {
let template = fs.readFileSync(
path.resolve(__dirname, "index.html"),
"utf-8"
);
template = await vite.transformIndexHtml("/", template);
res.setHeader("Content-Type", "text/html");
res.send(template);
});
app.listen(5173, () => {
console.log("go to", "http://localhost:5173");
});
}
createServer();