new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Deno · all subjects

http & networking

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

HTTP Server API: Deno.serve

Deno.serve is the native, higher-level HTTP Server API that supports HTTP/1.1 and HTTP/2. This is the preferred API to write HTTP servers in Deno. Takes a handler function called for each incoming request, expected to return a response or promise resolving to a response. By default listens on port 8000 but can be changed via options.

HTTP Server simple example with Deno.serve

Example of simple HTTP server: ```ts Deno.serve((_req) => { return new Response("Hello, World!"); }); ```

HTTP Server API: Deno.serveHttp

Deno.serveHttp is the native, low-level HTTP Server API that supports HTTP/1.1 and HTTP/2. This is a lower-level alternative to Deno.serve.

Networking functions: Deno.connect and Deno.listen

Deno.connect(options) connects to a hostname and port. Deno.listen(options) announces on a local transport address. Both are built-in functions for dealing with connections to network ports.

Extract URL pathname from request

To get the pathname from an incoming request, use: const url = new URL(request.url); const filepath = decodeURIComponent(url.pathname);. The decodeURIComponent function handles URL-encoded characters that may have been percent-encoded.

File server example with error handling

This example opens a file at the requested path and streams it, or returns 404 if not found: try { const file = await Deno.open("." + filepath, { read: true }); return new Response(file.readable); } catch { return new Response("404 Not Found", { status: 404 }); }

Import and use serveDir from @std/http in project

Add @std/http to deno.json with: deno add jsr:@std/http. Then import and use: import { serveDir } from "@std/http/file-server"; and call serveDir(req, { fsRoot: "path/to/files" }) to serve files from a specific directory.

serveDir conditional routing example

This example sets up routing where only requests to paths starting with "/static" are served from a directory: Deno.serve((req) => { const pathname = new URL(req.url).pathname; if (pathname.startsWith("/static")) { return serveDir(req, { fsRoot: "path/to/static/files/dir" }); } return new Response(); });

Give your agent this brain