File server

Concepts

  • Use Deno.open to read a file’s content in chunks.
  • Use the Deno standard library streams module to transform a Deno file into a ReadableStream.
  • Use Deno’s integrated HTTP server to run your own file server.

Overview

Sending files over the network is a common requirement. As seen in the Fetch Data example, because files can be of any size, it is important to use streams in order to prevent having to load entire files into memory.

Example

Command: deno run --allow-read --allow-net file_server.ts

  1. import { readableStreamFromReader } from "https://deno.land/std@$STD_VERSION/streams/mod.ts";
  2. // Start listening on port 8080 of localhost.
  3. const server = await Deno.listen({ port: 8080 });
  4. console.log("File server running on http://localhost:8080/");
  5. for await (const conn of server) {
  6. const httpConn = Deno.serveHttp(conn);
  7. for await (const requestEvent of httpConn) {
  8. serveFile(requestEvent);
  9. }
  10. }
  11. async function serveFile(requestEvent: Deno.RequestEvent) {
  12. // Use the request pathname as filepath
  13. const url = new URL(requestEvent.request.url);
  14. const filepath = decodeURIComponent(url.pathname);
  15. // Try opening the file
  16. let file;
  17. try {
  18. file = await Deno.open("." + filepath, { read: true });
  19. } catch {
  20. // If the file cannot be opened, return a "404 Not Found" response
  21. const notFoundResponse = new Response("404 Not Found", { status: 404 });
  22. await requestEvent.respondWith(notFoundResponse);
  23. return;
  24. }
  25. // Build a readable stream so the file doesn't have to be fully loaded into
  26. // memory while we send it
  27. const readableStream = readableStreamFromReader(file);
  28. // Build and send the response
  29. const response = new Response(readableStream);
  30. await requestEvent.respondWith(response);
  31. }

Using the std/http file server

The Deno standard library provides you with a file server so that you don’t have to write your own.

To use it, first install the remote script to your local file system. This will install the script to the Deno installation root’s bin directory, e.g. /home/alice/.deno/bin/file_server.

  1. deno install --allow-net --allow-read https://deno.land/std@$STD_VERSION/http/file_server.ts

You can now run the script with the simplified script name. Run it:

  1. $ file_server .
  2. Downloading https://deno.land/std@$STD_VERSION/http/file_server.ts...
  3. [...]
  4. HTTP server listening on http://0.0.0.0:4507/

Now go to http://0.0.0.0:4507/ in your web browser to see your local directory contents.

The complete list of options are available via:

  1. file_server --help

Example output:

  1. Deno File Server
  2. Serves a local directory in HTTP.
  3. INSTALL:
  4. deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
  5. USAGE:
  6. file_server [path] [options]
  7. OPTIONS:
  8. -h, --help Prints help information
  9. -p, --port <PORT> Set port
  10. --cors Enable CORS via the "Access-Control-Allow-Origin" header
  11. --host <HOST> Hostname (default is 0.0.0.0)
  12. -c, --cert <FILE> TLS certificate file (enables TLS)
  13. -k, --key <FILE> TLS key file (enables TLS)
  14. --no-dir-listing Disable directory listing
  15. All TLS options are required when one is provided.