-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_server.ts
33 lines (29 loc) · 907 Bytes
/
file_server.ts
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 {
serve,
ServerRequest,
Response,
} from 'https://deno.land/[email protected]/http/server.ts';
import { extname, posix } from 'https://deno.land/[email protected]/path/mod.ts';
const { stat, open, cwd } = Deno;
async function serveFile(req: ServerRequest, filePath: string) {
const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]);
const headers = new Headers();
headers.set('content-length', fileInfo.len.toString());
headers.set('content-type', 'text/plain; charset=utf-8');
const res = {
status: 200,
body: file,
headers,
};
return res;
}
window.onload = async function main() {
const addr = '0.0.0.0:4500';
const server = serve(addr);
console.log(`HTTP server listening on http://${addr}/`);
for await (const req of server) {
const res = await serveFile(req, 'public/index.html');
await req.respond(res);
res.body.close();
}
};