Skip to main content
On this page

deno serve runs a file as an HTTP server using Deno.serve(). The file must export a default object with a fetch handler. For a full guide on building HTTP servers, see Writing an HTTP Server.

Basic usage Jump to heading

server.ts
export default {
  fetch(_req: Request) {
    return new Response("Hello world!");
  },
} satisfies Deno.ServeDefaultExport;
›_
deno serve server.ts

By default, the server listens on port 8000. Override it with --port:

›_
deno serve --port=3000 server.ts

deno serve vs deno run Jump to heading

Both approaches start an HTTP server, and the difference is who calls Deno.serve():

  • With deno serve, Deno calls Deno.serve() for you. Your file just exports a default object with a fetch handler. Deno owns the listener, which lets it add features such as running multiple instances across threads with --parallel.
  • With deno run, you call Deno.serve() yourself inside the program. This gives you full control over the listen options and the surrounding code, and is the right choice when the server is one part of a larger program.

Use deno serve when the program is primarily an HTTP server and you want Deno to manage it; use deno run with Deno.serve() when you need to control how and when the server starts.

Default export shape Jump to heading

The file must export a default object that satisfies Deno.ServeDefaultExport. The object has two properties:

export interface ServeDefaultExport {
  fetch: ServeHandler;
  onListen?: (localAddr: Deno.Addr) => void;
}

fetch (required) Jump to heading

The fetch handler receives a standard Request and a ServeHandlerInfo object with connection metadata:

type ServeHandler = (
  request: Request,
  info: ServeHandlerInfo,
) => Response | Promise<Response>;

interface ServeHandlerInfo {
  remoteAddr: Deno.Addr; // remote address of the connection
  completed: Promise<void>; // resolves when the request completes
}

If the handler throws, the error is isolated to that request — the server continues serving.

onListen (optional) Jump to heading

Called once when the server starts listening. If omitted, a default message is logged to the console.

server.ts
export default {
  fetch(request, info) {
    const { hostname, port } = info.remoteAddr as Deno.NetAddr;
    console.log(`${request.method} ${request.url} from ${hostname}:${port}`);

    return new Response("Hello, World!", {
      headers: { "content-type": "text/plain" },
    });
  },

  onListen({ hostname, port }) {
    console.log(`Server running at http://${hostname}:${port}/`);
  },
} satisfies Deno.ServeDefaultExport;

Any other properties on the default export are silently ignored. If fetch is missing, no server starts. If fetch or onListen exist but are not functions, a TypeError is thrown.

Routing requests Jump to heading

Use the request URL to route to different handlers:

server.ts
export default {
  fetch(request: Request) {
    const url = new URL(request.url);

    if (url.pathname === "/api/health") {
      return Response.json({ status: "ok" });
    }

    return new Response("Not found", { status: 404 });
  },
} satisfies Deno.ServeDefaultExport;

Binding to a hostname Jump to heading

By default, deno serve listens on 0.0.0.0. Use --host to bind to a specific interface:

›_
deno serve --host=127.0.0.1 server.ts

Horizontal scaling Jump to heading

Run multiple server instances across CPU cores for better throughput:

›_
deno serve --parallel server.ts

Watch mode Jump to heading

Restart the server automatically when files change:

›_
deno serve --watch server.ts

Permissions Jump to heading

deno serve automatically allows the server to listen without requiring --allow-net. Additional permissions (like file reads) must be granted explicitly:

›_
deno serve --allow-read server.ts
Command line usage:
serve

Run a server

Options Jump to heading

--allow-scripts
Jump to heading

Allow running npm lifecycle scripts for the given packages Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).

--cached-only
Jump to heading

Require that remote dependencies are already cached.

Load certificate authority from PEM encoded file.

Enable type-checking. This subcommand does not type-check by default; pass --check=all to also type-check remote modules. Alternatively, use the 'deno check' subcommand.

Use this argument to specify custom conditions for npm package exports. You can also use DENO_CONDITIONS env var. .

Configure different aspects of deno including TypeScript, linting, and code formatting. Typically the configuration file will be called deno.json or deno.jsonc and automatically detected; in that case this flag is not necessary.

Load environment variables from local file Only the first environment variable with a given key is used. Existing process environment variables are not overwritten, so if variables with the same names already exist in the environment, their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one encountered is applied. This is determined by the order of the files you pass as arguments.

Set content type of the supplied file.

--frozen-lockfile
Jump to heading

Error out if lockfile is out of date.

The TCP address to serve on, defaulting to 0.0.0.0 (all interfaces).

Load import map file from local file or remote URL.

Activate inspector on host:port [default: 127.0.0.1:9229]. Host and port are optional. Using port 0 will assign a random free port.

--inspect-brk
Jump to heading

Activate inspector on host:port, wait for debugger to connect and break at the start of user script.

--inspect-publish-uid
Jump to heading
--inspect-wait
Jump to heading

Activate inspector on host:port and wait for debugger to connect before running user code.

Value of globalThis.location used by some web APIs.

Check the specified lock file. (If value is not provided, defaults to "./deno.lock").

--min-dep-age
Jump to heading

(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable).

Skip type-checking. If the value of "remote" is supplied, diagnostic errors from remote modules will be ignored.

--no-clear-screen
Jump to heading

Do not clear terminal screen when under watch mode.

--no-code-cache
Jump to heading

Disable V8 code cache feature.

--no-config
Jump to heading

Disable automatic loading of the configuration file.

Disable auto discovery of the lock file.

Do not resolve npm modules.

--no-remote
Jump to heading

Do not resolve remote modules.

--node-modules-dir
Jump to heading

Selects the node_modules directory mode for npm packages (not a path). One of: auto (create a local node_modules directory and install npm packages into it), manual (use the existing local node_modules directory, do not modify it), none (do not use a local node_modules directory; resolve npm packages from the global cache). Defaults to auto when the flag is passed without a value.

--node-modules-linker
Jump to heading

Sets the linker mode for npm packages (isolated or hoisted).

Open the browser on the address that the server is running on.

Run multiple server workers in parallel. Parallelism defaults to the number of available CPUs or the value of the DENO_JOBS environment variable.

The TCP port to serve on. Pass 0 to pick a random free port [default: 8000]

A list of files that will be executed before the main module.

--reload, -r
Jump to heading

Reload source code cache (recompile TypeScript). With no value, reloads everything. Pass a comma-separated list of specifiers to reload only those modules; npm: reloads all npm modules; npm:chalk reloads a single npm module; jsr:@std/http/file-server,jsr:@std/assert/assert-equals reloads specific modules.

A list of CommonJS modules that will be executed before the main module.

Set the random number generator seed.

--unsafely-ignore-certificate-errors
Jump to heading

DANGER: Disables verification of TLS certificates.

To see a list of all available flags use --v8-flags=--help Flags can also be set via the DENO_V8_FLAGS environment variable. Any flags set with this flag are appended after the DENO_V8_FLAGS environment variable.

Toggles local vendor folder usage for remote modules and a node_modules folder for npm packages.

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.

--watch-exclude
Jump to heading

Exclude provided files/patterns from watch mode.

--watch-hmr
Jump to heading

Watch for file changes and hot-replace modules. The process restarts if hot replacement fails. Local files from entry point module graph are watched by default. Additional paths might be watched by passing them as arguments to this flag.

Last updated on

Did you find what you needed?

Edit this page
Privacy policy