# Runtime and HTTP

## Run files and tasks

```bash
deno run main.ts
deno run --watch --allow-net main.ts
deno task dev
```

Use `deno run` for a direct entry point and `deno task` to standardize flags and permissions in `deno.json`. Team READMEs and CI should invoke tasks instead of duplicating argument sets.

## Use web-standard APIs

```ts title="main.ts"
const controller = new AbortController();

Deno.serve({ port: 8000, signal: controller.signal }, (request) => {
  const url = new URL(request.url);
  if (url.pathname === "/health") return Response.json({ ok: true });
  return new Response("Not found", { status: 404 });
});

Deno.addSignalListener("SIGTERM", () => controller.abort());
```

```bash
deno run --allow-net main.ts
```

Requests use standard `Request`, `Response`, `URL`, streams, and `fetch`. `Deno.serve` owns the listener; it does not replace authentication, rate limiting, or an operations platform.

## Environment and exit behavior

```ts
const port = Number(Deno.env.get("PORT") ?? "8000");
if (!Number.isInteger(port)) throw new Error("PORT must be an integer");
```

Grant only the required name with `--allow-env=PORT`. Verify exit codes, uncaught exceptions, and signal behavior in the actual deployment environment.

Official references: [Run code](https://docs.deno.com/runtime/run/) and [HTTP server](https://docs.deno.com/runtime/fundamentals/http_server/).
