Skip to content

Commit 0f4fbe3

Browse files
committed
Fix setFileHandler example
1 parent 3200010 commit 0f4fbe3

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

examples/custom_file_handler/custom_file_handler.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// deno run --allow-read --allow-write --allow-run --allow-net --allow-ffi custom_file_handler.ts
33

44
// To import from local (Debugging and Development)
5-
import { WebUI } from "../../mod.ts";
5+
// import { WebUI } from "../../mod.ts";
66

77
// To import from online `https://deno.land` (Production)
8-
// import { WebUI } from "https://deno.land/x/[email protected]/mod.ts";
8+
import { WebUI } from "https://deno.land/x/[email protected]/mod.ts";
99

1010
// Return HTTP header + file raw binary content
1111
const getFile = async (contentType: string, filename: string): Promise<Uint8Array> => {
@@ -18,6 +18,28 @@ const getFile = async (contentType: string, filename: string): Promise<Uint8Arra
1818
return response;
1919
};
2020

21+
// Set a custom files handler
22+
async function myFileHandler(myUrl: URL) {
23+
console.log(`File: ${myUrl.pathname}`);
24+
// Index example
25+
if (myUrl.pathname === '/index.html' || myUrl.pathname === '/') {
26+
return await getFile('text/html', 'index.html');
27+
}
28+
// Custom text string example
29+
if (myUrl.pathname === '/test') {
30+
return "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello";
31+
}
32+
// File examples
33+
if (myUrl.pathname === '/assets/test_app.js') {
34+
return await getFile('application/javascript', 'assets/test_app.js');
35+
}
36+
if (myUrl.pathname === '/assets/webui.jpeg') {
37+
return await getFile('image/jpeg', 'assets/webui.jpeg');
38+
}
39+
// Error 404 example
40+
return "HTTP/1.1 404 Not Found";
41+
};
42+
2143
// Create new window
2244
const myWindow = new WebUI();
2345

@@ -27,25 +49,12 @@ myWindow.bind("exit", () => {
2749
WebUI.exit();
2850
});
2951

30-
// Set a custom files handler
31-
myWindow.setFileHandler((myUrl: URL) => {
32-
console.log(`File: ${myUrl.pathname}`);
33-
34-
// Index example
35-
if ((myUrl.pathname === '/index.html') || (myUrl.pathname === '/')) {
36-
return getFile('text/html', 'index.html')
37-
}
38-
// Custom text string example
39-
if (myUrl.pathname === '/test') return "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello"
40-
// File example
41-
if (myUrl.pathname === '/assets/test_app.js') return getFile('application/javascript', 'assets/test_app.js')
42-
if (myUrl.pathname === '/assets/webui.jpeg') return getFile('image/jpeg', 'assets/webui.jpeg')
43-
// Error 404 example
44-
return "HTTP/1.1 404 Not Found"
45-
});
52+
// Set files handler
53+
// Note: Should be called before `.show()`
54+
myWindow.setFileHandler(myFileHandler);
4655

4756
// Show the window
48-
await myWindow.show('index.html')
57+
await myWindow.show('index.html');
4958

5059
// Wait until all windows get closed
5160
await WebUI.wait();

0 commit comments

Comments
 (0)