-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
58 lines (51 loc) · 1.97 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Start listening on port 8080 of localhost.
const server = Deno.listen({ port: 8080 });
console.log("File server running on http://localhost:8080/");
for await (const conn of server) {
handleHttp(conn).catch(console.error);
}
async function handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
// Use the request pathname as filepath
const url = new URL(requestEvent.request.url);
let filepath = decodeURIComponent(url.pathname);
// Default to serving index.html if filepath is root
if (filepath === '/') {
filepath = '/index.html';
}
// Try opening the file
let file;
try {
file = await Deno.open("." + filepath, { read: true });
} catch (error) {
// If the file cannot be opened, return a "404 Not Found" response
console.error(`Error opening file ${filepath}: ${error}`);
const notFoundResponse = new Response("404 Not Found", { status: 404 });
await requestEvent.respondWith(notFoundResponse);
continue;
}
// Determine content type based on file extension
let contentType = 'application/octet-stream';
const extension = filepath.split('.').pop();
if (extension === 'html') {
contentType = 'text/html';
} else if (extension === 'js') {
contentType = 'application/javascript';
} else if (extension === 'css') {
contentType = 'text/css';
} else if (extension === 'json') {
contentType = 'application/json';
} else if (extension === 'ico') {
contentType = 'image/x-icon';
}
// Build a readable stream so the file doesn't have to be fully loaded into
// memory while we send it
const readableStream = file.readable;
// Build and send the response
const responseHeaders = new Headers();
responseHeaders.set('Content-Type', contentType);
const response = new Response(readableStream, { headers: responseHeaders });
await requestEvent.respondWith(response);
}
}