diff --git a/docs/content/3.server/1.api.md b/docs/content/3.server/1.api.md index 0154438cd6..34ebb6dbad 100644 --- a/docs/content/3.server/1.api.md +++ b/docs/content/3.server/1.api.md @@ -1 +1,36 @@ # API Routes + +Nuxt will automatically read in any files in the `~/server/api` directory to create API endpoints. + +Each file should export a default function that handles api requests. It can return a promise or JSON data directly or use `res.end()`. API routes are powered by [h3](https://github.com/unjs/h3). + +**Example:** Hello world + +```js [server/api/hello.ts] +export default async (req, res) => 'Hello World' +``` + +See result on http://localhost:3000/api/hello + +**Example:** An async function + +```js [server/api/async.ts] +import express from 'express' + +export default async (req, res) => { + await someAsyncFunction() + + return { + someData: true + } +} +``` + +**Example:** Using NodeJS style + +```js [server/api/node.ts] +export default async (req, res) => { + res.statusCode = 200 + res.end('Works!') +} +``` diff --git a/docs/content/3.server/2.middleware.md b/docs/content/3.server/2.middleware.md index 2b1fa54a6a..06e99d76c3 100644 --- a/docs/content/3.server/2.middleware.md +++ b/docs/content/3.server/2.middleware.md @@ -1 +1,11 @@ # Server Middleware + +Nuxt will automatically read in any files in the `~/server/middleware` to create server middleware for your project. (These files will be run on every request, unliked [API routes](./api) that are mapped to their own routes.) + +Each file should export a default function that will handle a request. + +```js +export default async (req, res) => { + req.someValue = true +} +``` diff --git a/docs/content/3.server/3.storage.md b/docs/content/3.server/3.storage.md deleted file mode 100644 index e0e8ac414a..0000000000 --- a/docs/content/3.server/3.storage.md +++ /dev/null @@ -1 +0,0 @@ -# Storage diff --git a/docs/content/3.server/99.nitro.md b/docs/content/3.server/99.nitro.md deleted file mode 100644 index d3651cf384..0000000000 --- a/docs/content/3.server/99.nitro.md +++ /dev/null @@ -1 +0,0 @@ -# Nitro (advanced)