diff options
author | 2023-02-23 17:13:30 -0800 | |
---|---|---|
committer | 2023-02-23 17:13:30 -0800 | |
commit | f54300578b1edc7f67daddbfae29575cbf305264 (patch) | |
tree | 1437f3274122c011f879dca71f59a74d75a33fd0 /docs/quickstart.md | |
parent | 5929daeeae1f528abab31979a0a28bc87a03b1f4 (diff) | |
download | bun-f54300578b1edc7f67daddbfae29575cbf305264.tar.gz bun-f54300578b1edc7f67daddbfae29575cbf305264.tar.zst bun-f54300578b1edc7f67daddbfae29575cbf305264.zip |
Add documentation (#2148)bun-v0.5.7
* Add documentation
* Tweaks
* Fixes
* Rearrange
* Update
Diffstat (limited to 'docs/quickstart.md')
-rw-r--r-- | docs/quickstart.md | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/docs/quickstart.md b/docs/quickstart.md new file mode 100644 index 000000000..a6b68b42c --- /dev/null +++ b/docs/quickstart.md @@ -0,0 +1,117 @@ +Let's write a simple HTTP server using the built-in `Bun.serve` API. First, create a fresh directory. + +```bash +$ mkdir quickstart +$ cd quickstart +``` + +Run `bun init` to scaffold a new project. It's an interactive tool; for this tutorial, just press `enter` to accept the default answer for each prompt. + +```bash +$ bun init +bun init helps you get started with a minimal project and tries to +guess sensible defaults. Press ^C anytime to quit. + +package name (quickstart): +entry point (index.ts): + +Done! A package.json file was saved in the current directory. + + index.ts + + .gitignore + + tsconfig.json (for editor auto-complete) + + README.md + +To get started, run: + bun run index.ts +``` + +Since our entry point is a `*.ts` file, Bun generates a `tsconfig.json` for you. If you're using plain JavaScript, it will generate a [`jsconfig.json`](https://code.visualstudio.com/docs/languages/jsconfig) instead. + +## Run a file + +Open `index.ts` and paste the following code snippet, which implements a simple HTTP server with [`Bun.serve`](/docs/api/http). + +```ts +const server = Bun.serve({ + port: 3000, + fetch(req) { + return new Response(`Bun!`); + }, +}); + +console.log(`Listening on http://localhost:${server.port}...`); +``` + +Run the file from your shell. + +```bash +$ bun index.ts +Listening at http://localhost:3000... +``` + +Visit [http://localhost:3000](http://localhost:3000) to test the server. You should see a simple page that says "Bun!". + +## Run a script + +Bun can also execute `"scripts"` from your `package.json`. Add the following script: + +```json-diff + { + "name": "quickstart", + "module": "index.ts", + "type": "module", ++ "scripts": { ++ "start": "bun run index.ts", ++ }, + "devDependencies": { + "bun-types": "^0.4.0" + } + } +``` + +Then run it with `bun run start`. + +```bash +$ bun run start + $ bun run index.ts + Listening on http://localhost:4000... +``` + +{% callout %} +⚡️ **Performance** — With Bun's fast startup times, this script executes in roughly `6ms`. +By contrast `npm run` adds roughly `170ms` of overhead when executing scripts. +{% /callout %} + +## Install a package + +Let's make our server a little more interesting by installing a package. First install the `figlet` package and its type declarations. Figlet is a utility for converting strings into ASCII art. + +```bash +$ bun add figlet +$ bun add -d @types/figlet # TypeScript users only +``` + +Update `index.ts` to use `figlet` in the `fetch` handler. + +```ts-diff ++ import figlet from "figlet"; + + const server = Bun.serve({ + fetch() { ++ const body = figlet.textSync('Bun!'); ++ return new Response(body); +- return new Response(`Bun!`); + }, + port: 3000, + }); +``` + +Restart the server and refresh the page. You should see a new ASCII art banner. + +```txt + ____ _ + | __ ) _ _ _ __ | | + | _ \| | | | '_ \| | + | |_) | |_| | | | |_| + |____/ \__,_|_| |_(_) +``` |