diff options
author | 2023-07-31 12:20:23 -0700 | |
---|---|---|
committer | 2023-07-31 12:20:23 -0700 | |
commit | 404b90badc5856a74c06d04062c850003e28fed5 (patch) | |
tree | 7954623cf4a792db612d0bb229a227c2ff1e9fd8 /docs/guides/ecosystem | |
parent | 67599f97adc77141331a5f4fc39e4d058dc70b2a (diff) | |
download | bun-404b90badc5856a74c06d04062c850003e28fed5.tar.gz bun-404b90badc5856a74c06d04062c850003e28fed5.tar.zst bun-404b90badc5856a74c06d04062c850003e28fed5.zip |
Add ecosystem guides (#3847)
* Add ecosystem guides
* Update titles
* Rename stric
* Add unlink and fetch guides
* Add formdata guide
* Tweak title
* Moar
Diffstat (limited to 'docs/guides/ecosystem')
-rw-r--r-- | docs/guides/ecosystem/elysia.md | 31 | ||||
-rw-r--r-- | docs/guides/ecosystem/express.md | 40 | ||||
-rw-r--r-- | docs/guides/ecosystem/hono.md | 39 | ||||
-rw-r--r-- | docs/guides/ecosystem/mongoose.md | 2 | ||||
-rw-r--r-- | docs/guides/ecosystem/prisma.md | 2 | ||||
-rw-r--r-- | docs/guides/ecosystem/react.md | 30 | ||||
-rw-r--r-- | docs/guides/ecosystem/ssr-react.md | 42 | ||||
-rw-r--r-- | docs/guides/ecosystem/stric.md | 45 | ||||
-rw-r--r-- | docs/guides/ecosystem/vite.md | 2 |
9 files changed, 230 insertions, 3 deletions
diff --git a/docs/guides/ecosystem/elysia.md b/docs/guides/ecosystem/elysia.md new file mode 100644 index 000000000..b0231ff95 --- /dev/null +++ b/docs/guides/ecosystem/elysia.md @@ -0,0 +1,31 @@ +--- +name: Build an HTTP server using Elysia +--- + +[Elysia](https://elysiajs.com) is a Bun-first performance focused web framework that takes full advantage of Bun's HTTP, file system, and hot reloading APIs. Get started with `bun create`. + +```bash +$ bun create elysia myapp +$ cd myapp +$ bun run dev +``` + +--- + +To define a simple HTTP route and start a server with Elysia: + +```ts#server.ts +import { Elysia } from 'elysia' + +const app = new Elysia() + .get('/', () => 'Hello Elysia') + .listen(8080) + +console.log(`🦊 Elysia is running at on port ${app.server.port}...`) +``` + +--- + +Elysia is a full-featured server framework with Express-like syntax, type inference, middleware, file uploads, and plugins for JWT authentication, tRPC, and more. It's also is one of the [fastest Bun web frameworks](https://github.com/SaltyAom/bun-http-framework-benchmark). + +Refer to the Elysia [documentation](https://elysiajs.com/quick-start.html) for more information. diff --git a/docs/guides/ecosystem/express.md b/docs/guides/ecosystem/express.md new file mode 100644 index 000000000..55bd468bd --- /dev/null +++ b/docs/guides/ecosystem/express.md @@ -0,0 +1,40 @@ +--- +name: Build an HTTP server using Express +--- + +Express and other major Node.js HTTP libraries should work out of the box. Bun implements the [`node:http`](https://nodejs.org/api/http.html) and [`node:https`](https://nodejs.org/api/https.html) modules that these libraries rely on. + +{% callout %} +Refer to the [Runtime > Node.js APIs](/docs/runtime/nodejs-apis#node_http) page for more detailed compatibility information. +{% /callout %} + +```sh +$ bun add express +``` + +--- + +To define a simple HTTP route and start a server with Express: + +```ts#server.ts +import express from "express"; + +const app = express(); +const port = 8080; + +app.get("/", (req, res) => { + res.send("Hello World!"); +}); + +app.listen(port, () => { + console.log(`Listening on port ${port}...`); +}); +``` + +--- + +To start the server on `localhost`: + +```sh +$ bun server.ts +``` diff --git a/docs/guides/ecosystem/hono.md b/docs/guides/ecosystem/hono.md new file mode 100644 index 000000000..f0497da1f --- /dev/null +++ b/docs/guides/ecosystem/hono.md @@ -0,0 +1,39 @@ +--- +name: Build an HTTP server using Hono +--- + +[Hono](https://github.com/honojs/hono) is a lightweight ultrafast web framework designed for the edge. + +```ts +import { Hono } from "hono"; +const app = new Hono(); + +app.get("/", c => c.text("Hono!")); + +export default app; +``` + +--- + +Use `create-hono` to get started with one of Hono's project templates. Select `bun` when prompted for a template. + +```bash +$ bunx create-hono myapp +✔ Which template do you want to use? › bun +cloned honojs/starter#main to /path/to/myapp +✔ Copied project files +$ cd myapp +$ bun install +``` + +--- + +Then start the dev server and visit [localhost:3000](http://localhost:3000). + +```bash +$ bun run dev +``` + +--- + +Refer to Hono's guide on [getting started with Bun](https://hono.dev/getting-started/bun) for more information. diff --git a/docs/guides/ecosystem/mongoose.md b/docs/guides/ecosystem/mongoose.md index c2916ce82..406d2e77e 100644 --- a/docs/guides/ecosystem/mongoose.md +++ b/docs/guides/ecosystem/mongoose.md @@ -1,5 +1,5 @@ --- -name: Use MongoDB and Mongoose +name: Read and write data to MongoDB using Mongoose --- MongoDB and Mongoose work out of the box with Bun. This guide assumes you've already installed MongoDB and are running it as background process/service on your development machine. Follow [this guide](https://www.mongodb.com/docs/manual/installation/) for details. diff --git a/docs/guides/ecosystem/prisma.md b/docs/guides/ecosystem/prisma.md index 39627b806..82e79ec06 100644 --- a/docs/guides/ecosystem/prisma.md +++ b/docs/guides/ecosystem/prisma.md @@ -1,5 +1,5 @@ --- -name: Use Prisma +name: Get started using Prisma --- Prisma works our of the box with Bun. First, create a directory and initialize it with `bun init`. diff --git a/docs/guides/ecosystem/react.md b/docs/guides/ecosystem/react.md new file mode 100644 index 000000000..b712e210e --- /dev/null +++ b/docs/guides/ecosystem/react.md @@ -0,0 +1,30 @@ +--- +name: Use React and JSX +--- + +React just works with Bun. Bun supports `.jsx` and `.tsx` files out of the box. Bun's internal transpiler converts JSX syntax into vanilla JavaScript before execution. + +```tsx#react.tsx +function Component(props: {message: string}) { + return ( + <body> + <h1 style={{color: 'red'}}>{props.message}</h1> + </body> + ); +} + +console.log(<Component message="Hello world!" />); +``` + +--- + +Bun implements special logging for JSX to make debugging easier. + +```bash +$ bun run react.tsx +<Component message="Hello world!" /> +``` + +--- + +Refer to [Runtime > JSX](/docs/runtime/jsx) for complete documentation on configuring JSX. diff --git a/docs/guides/ecosystem/ssr-react.md b/docs/guides/ecosystem/ssr-react.md new file mode 100644 index 000000000..59d37db46 --- /dev/null +++ b/docs/guides/ecosystem/ssr-react.md @@ -0,0 +1,42 @@ +--- +name: Server-side render (SSR) a React component +--- + +To render a React component to an HTML stream server-side (SSR): + +```tsx +import { renderToReadableStream } from "react-dom/server"; + +function Component(props: { message: string }) { + return ( + <body> + <h1>{props.message}</h1> + </body> + ); +} + +const stream = await renderToReadableStream( + <Component message="Hello from server!" />, +); +``` + +--- + +Combining this with `Bun.serve()`, we get a simple SSR HTTP server: + +```tsx +Bun.serve({ + async fetch() { + const stream = await renderToReadableStream( + <Component message="Hello from server!" />, + ); + return new Response(stream, { + headers: { "Content-Type": "text/html" }, + }); + }, +}); +``` + +--- + +React `18.3` and later includes an [SSR optimization](https://github.com/facebook/react/pull/25597) that takes advantage of Bun's "direct" `ReadableStream` implementation. diff --git a/docs/guides/ecosystem/stric.md b/docs/guides/ecosystem/stric.md new file mode 100644 index 000000000..309a85709 --- /dev/null +++ b/docs/guides/ecosystem/stric.md @@ -0,0 +1,45 @@ +--- +name: Build an HTTP server using StricJS +--- + +[StricJS](https://github.com/bunsvr) is a minimalist, fast web framework for Bun. Use `bun init` to create an empty project. + +```bash +$ mkdir myapp +$ cd myapp +$ bun init +$ bun add @stricjs/router +``` + +--- + +To implement a simple HTTP server with StricJS: + +```ts#index.ts +import { Router } from '@stricjs/router'; + +export default new Router() + .get('/', () => new Response('Hi')); +``` + +--- + +To serve static files from `/public/*`: + +```ts#index.ts +export default new Router() + .get('/', () => new Response('Hi')) + .get('/public/*', stream('.')); +``` + +--- + +Run the file in watch mode to start the development server. + +```bash +$ bun --watch run index.ts +``` + +--- + +For more info, see Stric's [documentation](https://stricjs.gitbook.io/docs). diff --git a/docs/guides/ecosystem/vite.md b/docs/guides/ecosystem/vite.md index cee5e6013..62c116e02 100644 --- a/docs/guides/ecosystem/vite.md +++ b/docs/guides/ecosystem/vite.md @@ -1,5 +1,5 @@ --- -name: Use Vite +name: Build a frontend using Vite --- {% callout %} |