Your first Deno project
Build a small maintainable project with main.ts, deno.json, and a test
Initialize a project:
deno init hello-deno
cd hello-deno
hello-deno/
├── deno.json
├── main.ts
└── main_test.ts
Make main.ts expose a testable handler:
export function handler(request: Request): Response {
const url = new URL(request.url);
return Response.json({ message: "Hello Deno", path: url.pathname });
}
if (import.meta.main) {
Deno.serve({ port: 8000 }, handler);
}
Record team commands in deno.json:
{
"tasks": {
"dev": "deno run --watch --allow-net=0.0.0.0:8000 main.ts",
"check": "deno fmt --check && deno lint && deno check main.ts main_test.ts",
"test": "deno test"
}
}
deno task check
deno task test
deno task dev
Listening with Deno.serve needs network permission. Unit tests that call handler() directly do not open a port, so they do not need -A.
Next: permissions, web stack selection, and project blueprints.