ThomasReggi
ThomasReggi

Reputation: 59455

How can I have Deno's std http file_server watch files?

I'm running an example file-server with a simple index.html file, I want the script to re-run when changes are made within the directory, how can I do that?

deno run --allow-net --allow-read --watch https://deno.land/[email protected]/http/file_server.ts ./

Upvotes: 2

Views: 649

Answers (1)

jsejcksn
jsejcksn

Reputation: 33788

You can provide one or more path values for the watch argument when using deno run in order watch additional files outside the module graph. For example, use

deno run —-watch=. module.ts

to watch all files recursively in the current working directory.

You can use the deno help command to get information about the command you want to use (in this case run). This is how I answered your question:

% deno --version
deno 1.26.2 (release, x86_64-apple-darwin)
v8 10.7.193.16
typescript 4.8.3

% deno help run
---snip---
USAGE:
    deno run [OPTIONS] <SCRIPT_ARG>...

ARGS:
    <SCRIPT_ARG>...
            Script arg

OPTIONS:
---snip---
        --watch[=<FILES>...]
            Watch for file changes and restart process automatically.
            Local files from entry point module graph are watched by default.
            Additional paths might be watched by passing them as arguments to
            this flag.

However in the case of the static file server module that you asked about, there's no real benefit to reloading the server process as it just serves static files: any time you request a static file, you always get the latest version.

Perhaps you're looking for "hot/live reload" behavior in the browser client. This is a different pattern: a coordinated effort between the JavaScript in the page and the server — and that’s not something that’s supported by the module you asked about.

Upvotes: 3

Related Questions