diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-04-24 15:45:49 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-24 19:45:49 +0000 |
commit | 2f8825a935bfdf21ca592284556cd86c1552ac8d (patch) | |
tree | 68beddeb5547861212be188f126113e6b1afc1df /cli/args/mod.rs | |
parent | c1bd9503dd0288a3c209ca2724d2a1de9d5d122b (diff) |
feat: Add `deno serve` subcommand (#23511)
By default, `deno serve` will assign port 8000 (like `Deno.serve`).
Users may choose a different port using `--port`.
`deno serve /tmp/file.ts`
`server.ts`:
```ts
export default {
fetch(req) {
return new Response("hello world!\n");
},
};
```
Diffstat (limited to 'cli/args/mod.rs')
-rw-r--r-- | cli/args/mod.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs index aa3622d09..bb0ef1aba 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -61,6 +61,7 @@ use std::env; use std::io::BufReader; use std::io::Cursor; use std::net::SocketAddr; +use std::num::NonZeroU16; use std::num::NonZeroUsize; use std::path::Path; use std::path::PathBuf; @@ -1022,6 +1023,22 @@ impl CliOptions { } } + pub fn serve_port(&self) -> Option<NonZeroU16> { + if let DenoSubcommand::Serve(flags) = self.sub_command() { + Some(flags.port) + } else { + None + } + } + + pub fn serve_host(&self) -> Option<String> { + if let DenoSubcommand::Serve(flags) = self.sub_command() { + Some(flags.host.clone()) + } else { + None + } + } + pub fn enable_future_features(&self) -> bool { *DENO_FUTURE } @@ -1062,6 +1079,10 @@ impl CliOptions { .map_err(AnyError::from) } } + DenoSubcommand::Serve(run_flags) => { + resolve_url_or_path(&run_flags.script, self.initial_cwd()) + .map_err(AnyError::from) + } _ => { bail!("No main module.") } |