mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
c75c03e0bb
Co-authored-by: Alexander Lichter <manniL@gmx.net> Co-authored-by: Pooya Parsa <pyapar@gmail.com>
37 lines
813 B
Markdown
37 lines
813 B
Markdown
# 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 Node.js style
|
||
|
||
```js [server/api/node.ts]
|
||
export default async (req, res) => {
|
||
res.statusCode = 200
|
||
res.end('Works!')
|
||
}
|
||
```
|