### Install dependencies and start the dev server Source: https://github.com/dokob0512/bun/blob/main/docs/guides/ecosystem/hono.mdx Navigate into the project directory, install the necessary dependencies using Bun, and then start the development server. ```bash cd myapp bun install ``` ```bash bun run dev ``` -------------------------------- ### Global installation configuration Source: https://github.com/dokob0512/bun/blob/main/docs/pm/cli/add.mdx Example `bunfig.toml` configuration for global installation directories. ```toml [install] # where `bun add --global` installs packages globalDir = "~/.bun/install/global" # where globally-installed package bins are linked globalBinDir = "~/.bun/bin" ``` -------------------------------- ### Project Setup and Testing Source: https://github.com/dokob0512/bun/blob/main/packages/bun-plugin-yaml/README.md Commands for setting up the project by installing dependencies and running tests. ```bash $ bun install # project setup ``` ```bash $ bun test # run tests ``` -------------------------------- ### Start a WebSocket server Source: https://github.com/dokob0512/bun/blob/main/docs/runtime/http/websockets.mdx This example shows how to start a basic WebSocket server using Bun.serve(). Incoming requests are upgraded to WebSocket connections in the fetch handler, and socket handlers are declared in the `websocket` parameter. ```APIDOC ## Start a WebSocket server ### Description This example shows how to start a basic WebSocket server using `Bun.serve()`. Incoming requests are upgraded to WebSocket connections in the `fetch` handler, and socket handlers are declared in the `websocket` parameter. ### Method `Bun.serve()` ### Parameters #### `fetch` handler - `req` (Request): The incoming request object. - `server` (BunServer): The Bun server instance. #### `websocket` handlers - `message(ws, message)`: Called when a message is received from a client. - `open(ws)`: Called when a new WebSocket connection is opened. - `close(ws, code, message)`: Called when a WebSocket connection is closed. - `drain(ws)`: Called when the WebSocket is ready to receive more data. ### Request Example ```ts Bun.serve({ fetch(req, server) { // upgrade the request to a WebSocket if (server.upgrade(req)) { return; // do not return a Response } return new Response("Upgrade failed", { status: 500 }); }, websocket: {}, }); ``` ### Response Example ```ts Bun.serve({ fetch(req, server) {}, websocket: { message(ws, message) {}, open(ws) {}, close(ws, code, message) {}, drain(ws) {}, }, }); ``` ``` -------------------------------- ### Basic Server Setup with Routes Source: https://github.com/dokob0512/bun/blob/main/docs/runtime/http/server.mdx Demonstrates how to start a Bun HTTP server and define various types of routes, including static, dynamic, method-specific, wildcard, and redirects. ```APIDOC ## Bun.serve with Routes ### Description Starts an HTTP server with predefined routes for handling different requests. ### Method `Bun.serve(options)` ### Parameters #### Options Object - **routes** (object) - Defines the routes for the server. - **"/api/status"** (Response) - Handles requests to `/api/status`. - **"/users/:id"** (function) - Handles dynamic routes like `/users/123`. - **"/api/posts"** (object) - Handles different HTTP methods for `/api/posts`. - **GET** (function) - Handler for GET requests. - **POST** (function) - Handler for POST requests. - **"/api/*"** (Response) - Wildcard route for paths starting with `/api/`. - **"/blog/hello"** (Response) - Handles redirects. - **"/favicon.ico"** (File) - Serves a file. - **fetch** (function) - Fallback handler for unmatched routes. ### Request Example ```ts const server = Bun.serve({ routes: { "/api/status": new Response("OK"), "/users/:id": req => { return new Response(`Hello User ${req.params.id}!`); }, "/api/posts": { GET: () => new Response("List posts"), POST: async req => { const body = await req.json(); return Response.json({ created: true, ...body }); }, }, "/api/*": Response.json({ message: "Not found" }, { status: 404 }), "/blog/hello": Response.redirect("/blog/hello/world"), "/favicon.ico": Bun.file("./favicon.ico"), }, fetch(req) { return new Response("Not Found", { status: 404 }); }, }); console.log(`Server running at ${server.url}`); ``` ### Response #### Success Response (200) Depends on the route handler. #### Response Example ```json { "message": "OK" } ``` ``` -------------------------------- ### Setup Benchmarks with Bun Source: https://github.com/dokob0512/bun/blob/main/bench/test/README.md Install dependencies and set up benchmark suites for parallel and isolate testing. ```sh cd bench/test bun install # for vitest bun parallel/setup.ts bun isolate-cache/setup.ts ``` -------------------------------- ### Initialize Astro App with Bun Source: https://github.com/dokob0512/bun/blob/main/docs/guides/ecosystem/astro.mdx Use `bun create astro` to start a new Astro project. Bun automatically detects and uses itself for dependency installation. ```sh bun create astro ``` -------------------------------- ### Comprehensive Test Configuration Example Source: https://github.com/dokob0512/bun/blob/main/docs/test/configuration.mdx A complete example demonstrating various test configuration options in `bunfig.toml`, including install settings, test discovery, execution, coverage, and reporter settings. ```toml [install] # Install settings inherited by tests registry = "https://registry.npmjs.org/" exact = true [test] # Test discovery root = "src" preload = ["./test-setup.ts", "./global-mocks.ts"] pathIgnorePatterns = ["vendor/**", "submodules/**"] # Execution settings timeout = 10000 smol = true # Coverage configuration coverage = true coverageReporter = ["text", "lcov"] coverageDir = "./coverage" coverageThreshold = { lines = 0.85, functions = 0.90, statements = 0.80 } coverageSkipTestFiles = true coveragePathIgnorePatterns = [ "**/*.spec.ts", "src/utils/**", "*.config.js", "generated/**" ] # Advanced coverage settings coverageIgnoreSourcemaps = false # Reporter configuration [test.reporter] junit = "./reports/junit.xml" ``` -------------------------------- ### Install Bun Natively Source: https://github.com/dokob0512/bun/blob/main/CONTRIBUTING.md Installs Bun directly using the official installation script. This is the recommended method for native installation. ```bash $ curl -fsSL https://bun.com/install | bash ``` -------------------------------- ### Install Dependencies for SolidStart App Source: https://github.com/dokob0512/bun/blob/main/docs/guides/ecosystem/solidstart.mdx After creating the SolidStart project, navigate into the project directory and install the necessary dependencies using `bun install`. ```sh cd my-app bun install ``` -------------------------------- ### Bun Command-Line Tool Examples Source: https://github.com/dokob0512/bun/blob/main/README.md Bun provides a versatile command-line interface for various development tasks. Use `bun test` to run tests, `bun run