Skip to content

Commit

Permalink
add runtimes and more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 7, 2023
1 parent 07f8d76 commit 5fb63b3
Show file tree
Hide file tree
Showing 35 changed files with 722 additions and 310 deletions.
13 changes: 13 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createApp, createRouter, eventHandler } from "h3";

export const app = createApp();

const router = createRouter();
app.use(router);

router.get(
"/",
eventHandler((event) => {

Check warning on line 10 in app.ts

View workflow job for this annotation

GitHub Actions / autofix

'event' is defined but never used

Check warning on line 10 in app.ts

View workflow job for this annotation

GitHub Actions / ci (16)

'event' is defined but never used

Check warning on line 10 in app.ts

View workflow job for this annotation

GitHub Actions / ci (18)

'event' is defined but never used

Check warning on line 10 in app.ts

View workflow job for this annotation

GitHub Actions / ci (20)

'event' is defined but never used
return { message: "⚡️ Tadaa!" };
}),
);

Check warning on line 13 in app.ts

View check run for this annotation

Codecov / codecov/patch

app.ts#L2-L13

Added lines #L2 - L13 were not covered by tests
64 changes: 59 additions & 5 deletions docs/content/1.getting-started/1.index.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,64 @@
---
title: Introduction
description: Welcome to H3 Docs
title: Quick start
description:
---

H3 (pronounced as /eɪtʃθriː/, like h-3) is a minimal h(ttp) framework built for high performance and portability.
Getting started with H3 is super simple and requires no dependencies or template! In this example we will create a Hello World application using h3 router.

👉 [Online Playground](https://stackblitz.com/github/unjs/h3/tree/main/playground)
First create an `app.ts` file:

👉 [Online Examples Playground](https://stackblitz.com/github/unjs/h3/tree/main/examples)
```ts [app.ts]
import { createApp, createRouter, eventHandler } from "h3";

export const app = createApp();

const router = createRouter();
app.use(router);

router.get(
"/",
eventHandler((event) => {
return { message: "⚡️ Tadaa!" };
}),
);
```

Now run the development server using [unjs/listhen](https://listhen.unjs.io):

```bash [terminal]
npx --yes listhen -w --open ./app.ts
```

And tadaa! We have a web server running locally.

## What happend?

Okay, let's now break down our hello world example:

We first created an H3 app instance using `createApp()`. `app` is a tiny server capable of matching requests, generating response and handling lifecycle hooks (such as errors):

```ts
export const app = createApp();
```

Then we create an H3 router instance that can match route patterns and http methods using [unjs/radix3](https://radix3.unjs.io) and register it for app as main handler:

```ts
const router = createRouter();

app.use(router);
```

Now it it time to add our first endpoint. In H3, request handlers can be defined using `defineEventHandler` or `eventHandler` helpers (they are aliases). Using wrappers, h3 can supercharge your code with better typehints and future compatibility.

```ts
eventHandler((event) => {});
```

What is beatiful in h3 is that all you have to do to make a reponse, is to simply return it! Responses can be simple string, JSON objects, data buffers, streams or standard [Web Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response).

```ts
return { message: "⚡️ Tadaa!" };
```

Finally, we use [unjs/listhen](https://listhen.unjs.io) CLI using npx to auto install it. Listhen will automatically setup and start our webserver with zero configuration and adds on-the-fly TypeScript support to your experience!
52 changes: 12 additions & 40 deletions docs/content/1.getting-started/2.installation.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
---
title: Installation
description: Get started
description:
---

```bash
# Using npm
npm install h3

# Using yarn
yarn add h3
You can install `h3` dependency from [npm registry](https://www.npmjs.com/package/h3) to your project:

# Using pnpm
pnpm add h3
::code-group

# Using bun
bun add h3
```bash [npm]
npm install h3
```

## Nightly Channel

If you are directly using `h3` as a dependency:

```json
{
"dependencies": {
"h3": "npm:h3-nightly@latest"
}
}
```bash [bun]
bun i h3
```

If you are using a framework ([Nuxt](https://nuxt.com/) or [Nitro](https://nitro.unjs.io/)) that is using `h3`:

pnpm and yarn:

```json
{
"resolutions": {
"h3": "npm:h3-nightly@latest"
}
}
```bash [pnpm]
pnpm add h3
```

npm:

```json
{
"overrides": {
"h3": "npm:h3-nightly@latest"
}
}
```bash [yarn]
yarn add h3
```

**Note:** Make sure to recreate lockfile and `node_modules` after reinstall to avoid hoisting issues.
::
124 changes: 0 additions & 124 deletions docs/content/1.getting-started/3.usage.md

This file was deleted.

Loading

0 comments on commit 5fb63b3

Please sign in to comment.