-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
33 lines (25 loc) · 1.12 KB
/
main.mjs
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
import { existsSync, statSync } from "node:fs";
import { readdir } from "node:fs/promises";
import { join, normalize } from 'node:path';
const enableIndex = !!process.env.ENABLE_INDEX;
const noIndexFile = !existsSync("./dist/index.html");
const rootDir = '/home/app/dist';
export default async function (request, response, next) {
const url = new URL(request.url, 'http://local');
if (url.pathname === 'favicon.ico') {
return next();
}
const isGet = request.method === "GET";
const relativePath = normalize(url.pathname);
const fullPath = join(rootDir, relativePath);
const showList = (enableIndex && noIndexFile && isGet && request.url === "/") ||
(enableIndex && isGet && existsSync(fullPath) && statSync(fullPath).isDirectory() && !existsSync(join(fullPath, 'index.html')));
if (showList) {
const list = await readdir(fullPath, { withFileTypes: true });
const files = list.map(({ name }) => `<a href="${join(relativePath, name)}" title="Open ${name}">${name}</a>`);
const html = "<h1>Files at </h1><hr/><nav>" + files.join("<br/>") + "</nav>";
response.end(html);
return;
}
next();
}