Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
On this page

Write a file server

A file server listens for incoming HTTP requests and serves files from the localfile system. This tutorial demonstrates how to create a simple file server usingDeno's built-infile system APIs.

Write a simple File ServerJump to heading

To start, create a new file calledfile-server.ts.

We'll use Deno's built inHTTP server to listen forincoming requests. In your newfile-server.ts file, add the following code:

file-server.ts
Deno.serve({ hostname:"localhost", port:8080},async(request)=>{const url=newURL(request.url);const filepath=decodeURIComponent(url.pathname);},);

If you're not familiar with theURL object, you can learn more about it intheURL APIdocumentation. ThedecodeURIComponent functionis used to decode the URL-encoded path, in the case that characters have beenpercent-encoded.)

Open a file and stream its contentsJump to heading

When a request is received, we'll attempt to open the file specified in therequest URL withDeno.open.

If the requested file exists, we'll convert it into a readable stream of datawith theReadableStream API,and stream its contents to the response. We don't know how large the requestedfile might be, so streaming it will prevent memory issues when serving largefiles or multiple requests concurrently.

If the file does not exist, we'll return a "404 Not Found" response.

In the body of the request handler, below the two variables, add the followingcode:

try{const file=await Deno.open("."+ filepath,{ read:true});returnnewResponse(file.readable);}catch{returnnewResponse("404 Not Found",{ status:404});}

Run the file serverJump to heading

Run your new file server with thedeno run command, allowing read access andnetwork access:

deno run --allow-read=. --allow-net file-server.ts

Using the file server provided by the Deno Standard LibraryJump to heading

Writing a file server from scratch is a good exercise to understand how Deno'sHTTP server works. However, writing production ready file server from scratchcan be complex and error-prone. It's better to use a tested and reliablesolution.

The Deno Standard Library provides you with afile server so that you don't haveto write your own.

To use it, first install the remote script to your local file system:

# Deno 1.xdenoinstall --allow-net --allow-read jsr:@std/http/file-server# Deno 2.xdenoinstall--global --allow-net --allow-read jsr:@std/http/file-server

This will install the script to the Deno installation root, e.g./home/user/.deno/bin/file-server.

You can now run the script with the simplified script name:

$ file-server.Listening on:- Local: http://0.0.0.0:8000

To see the complete list of options available with the file server, runfile-server --help.

If you visithttp://0.0.0.0:8000/ in your web browseryou will see the contents of your local directory.

Using the @std/http file server in a Deno projectJump to heading

To use the file-server in aDeno project, you can add it to yourdeno.json file with:

denoadd jsr:@std/http

And then import it in your project:

file-server.ts
import{ serveDir}from"@std/http/file-server";Deno.serve((req)=>{const pathname=newURL(req.url).pathname;if(pathname.startsWith("/static")){returnserveDir(req,{      fsRoot:"path/to/static/files/dir",});}returnnewResponse();});

This code will set up an HTTP server withDeno.serve. When a request comes in,it checks if the requested path starts with “/static”. If so, it serves filesfrom the specified directory. Otherwise, it responds with an empty response.

🦕 Now you know how to write your own simple file server, and how to use thefile-server utility provided by the Deno Standard Library. You're equipped totackle a whole variety of tasks - whether it’s serving static files, handlinguploads, transforming data, or managing access control - you're ready to servefiles with Deno.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp