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

Bun · Runtime · all subjects

http server & routing

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

reusePort option for concurrent HTTP servers

The reusePort option in Bun.serve() enables multiple HTTP servers to share the same port across multiple processes. Incoming requests are load balanced across the processes. This option uses the Linux SO_REUSEPORT and SO_REUSEADDR socket options to ensure fair load balancing.

reusePort platform support limitation

The reusePort option is Linux only. Windows and macOS ignore the reusePort option due to operating system limitations with SO_REUSEPORT socket option.

Example: HTTP server cluster with reusePort

```ts import { serve } from "bun"; const id = Math.random().toString(36).slice(2); serve({ port: process.env.PORT || 8080, development: false, reusePort: true, async fetch(request) { return new Response("Hello from Bun #" + id + "!\n"); }, }); ``` This example demonstrates a basic HTTP server configured with reusePort enabled to share a port across multiple processes.

Request.formData() parsing

The .formData() method is called on an incoming Request object and asynchronously parses the request contents into a FormData instance. This method is used to handle multipart/form-data requests.

FormData.get() to extract fields

The FormData.get() method is used to extract individual fields from a FormData instance. String fields return strings, and file fields return Blob objects.

HTML form for multipart file upload

An HTML form for file uploads must use method="post" and enctype="multipart/form-data" attributes. File input fields return Blob objects when accessed via FormData.get() on the server. The form's action attribute specifies the endpoint where the form data is POSTed.

Hot reload HTTP server with --hot flag

The --hot flag runs a file with hot reloading enabled. When any module or file changes, Bun re-runs the file. Use the command: bun --hot run index.ts

Bun.serve hot reload behavior

Bun detects when you are running an HTTP server with Bun.serve(). It reloads your fetch handler when source files change, without restarting the bun process. This makes hot reloads nearly instantaneous.

Hot reload does not reload browser page

Hot reloading in Bun does not reload the page in your browser.

Basic Bun.serve with hot reload example

Bun.serve({ port: 3000, fetch(req) { return new Response("Hello world"); } }) — This example shows a basic HTTP server that will have its fetch handler reloaded when running with the --hot flag.

Give your agent this brain