Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: more improvements #985

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/content/1.guide/1.introduction/0.getting-started.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Getting Started
# Getting Started

Let's create a new Nitro app!
Let's create a new Nitro app in few steps.

## Play online

Expand Down
49 changes: 31 additions & 18 deletions docs/content/1.guide/1.introduction/3.routing.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
# Route Handling
---
title: Routing
---

Nitro supports file-based routing for your API routes.
# Filesystem Routing

Handler files inside `routes/` and `api/` directory will be automatically mapped to [unjs/h3](https://github.com/unjs/h3) routes.
Nitro supports file-based routing for your API routes.

**Note:** `api/` is a shortcut for `routes/api` as a common prefix. However, please note that some deployment providers use `api/` directory for their API format. You can simply use the `routes/api` or `srcDir` option to move everything under `src/` or `server/` directory.
Handler files inside `routes/` directory will be automatically mapped to [unjs/h3](https://github.com/unjs/h3) routes.

## Usage

Check out [h3 JSDocs](https://www.jsdocs.io/package/h3#package-index-functions) for all available utilities.

## Examples
```md
routes/
api/
test.ts <-- /api/test
hello.ts <-- /hello
```

**Example:** Simple API route
### Simple route

```ts
// routes/test.ts
export default eventHandler(() => 'Hello World!')
```

**Example:** API route with params
### Route with params

```js
// routes/hello/[name].ts
export default eventHandler(event => `Hello ${event.context.params.name}!`)
```

**Example:** API route with a specific HTTP request method (get, post, put, delete, options and so on)
### Specific request method

```js
// routes/user.get.ts
export default eventHandler(async event => {
API route with a specific HTTP request method (get, post, put, delete, options and so on).

::code-group
```js [GET]
// routes/users/[id].get.ts
export default eventHandler(async (event) => {
const { id } = event.context.params
// TODO: fetch user by id
return `User profile!`
})
```

```js
// routes/user.post.ts
```js [POST]
// routes/users.post.ts
export default eventHandler(async event => {
const body = await readBody(event)
// TODO: Handle body and update user
return `User updated!`
// TODO: Handle body and add user
return { updated: true }
})
```
::

Check out [h3 JSDocs](https://www.jsdocs.io/package/h3#package-index-functions) for all available utilities like `readBody`.

**Example:** Catch all page
### Catch all route

```js
// routes/[...].ts
Expand Down
28 changes: 14 additions & 14 deletions docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@ snippet:
⚗️ Nitro

#description
Build and Deploy Universal JavaScript Servers, everywhere.
Build and Deploy Universal JavaScript Servers, everywhere. :br The engine powering [Nuxt 3](https://nuxt.com) and open to anyone.
::

::card-grid
::card{icon=🐇}
#title
Rapid Development
#description
Zero config setup with hot module replacement
Zero config setup with hot module replacement for server code.
::

::card{icon=😌}
#title
Multi-Provider
Deploy Everywhere
#description
Deploy same codebase to any provider without extra config
Deploy the same codebase to any [deployment provider](/deploy) with no extra config.
::

::card{icon=💼}
#title
Portable and Compact
#description
Say goodbye to node_modules
Say goodbye to `node_modules`, output size is less than 1MB.
::

::card{icon=📁}
#title
Filesystem Routing
#description
Automatically registers Server and API routes
[Automatically registers](/guide/introduction/routing) server and API routes.
::

::card{icon=🤏}
Expand All @@ -68,48 +68,48 @@ Build and Deploy Universal JavaScript Servers, everywhere.
#title
Code-Splitting
#description
Async chunk loading for fast server startup time
Async chunk loading for fast server startup time and answer.
::

::card{icon=👕}
#title
TypeScript
#description
TypeScript support out of the box with few more goodies
TypeScript support out of the box with few more goodies.
::

::card{icon=💾}
#title
Storage Layer
#description
Multi driver and platform-agnostic storage system
Multi driver and platform-agnostic storage system.
::

::card{icon=💰}
#title
Cache API
#description
Powerful built-in caching API
Powerful built-in caching API.
::

::card{icon=🐱}
#title
Hackable
#description
Built to be customized
Built to be customized with the [plugins](/guide/advanced/plugins) hooks system.
::

::card{icon=✨}
#title
Auto Imports
#description
Automatically import utilities for the lazy folks and a minimal codebase
Automatically import utilities for a minimal and clean codebase. Only the used ones will be added to the final bundle.
::

::card{icon=🏛️}
#title
Backward compatible
Backward Compatible
#description
Best-effort compatibility for using legacy npm packages, CommonJS and mocking Node.js modules for workers
So you can use legacy npm packages, CommonJS and mocking Node.js modules for workers.
::
::