2021-10-11 12:57:54 +00:00
---
icon: IconDirectory
title: server
head.title: Server directory
---
# Server directory
2022-03-16 11:16:05 +00:00
Nuxt uses the `server/` directory to create any backend logic for your application. It supports HMR and powerful features.
2021-10-11 12:57:54 +00:00
2021-10-11 22:47:43 +00:00
The `server/` directory contains API endpoints and server middleware for your project.
2021-10-11 12:57:54 +00:00
## API Routes
Nuxt will automatically read in any files in the `~/server/api` directory to create API endpoints.
2021-11-21 12:31:44 +00:00
Each file should export a default function that handles API requests. It can return a promise or JSON data directly (or use `res.end()` ).
2021-10-11 12:57:54 +00:00
### Examples
#### Hello world
```js [server/api/hello.ts]
export default (req, res) => 'Hello World'
```
2021-11-21 12:31:44 +00:00
See the result on < http: / / localhost:3000 / api / hello > .
2021-10-11 12:57:54 +00:00
#### Async function
```js [server/api/async.ts]
export default async (req, res) => {
await someAsyncFunction()
return {
someData: true
}
}
```
**Example:** Using Node.js style
```ts [server/api/node.ts]
import type { IncomingMessage, ServerResponse } from 'http'
export default async (req: IncomingMessage, res: ServerResponse) => {
res.statusCode = 200
res.end('Works!')
}
```
2022-01-17 10:56:12 +00:00
#### Accessing req data
```js
import { useBody, useCookies, useQuery } from 'h3'
export default async (req, res) => {
2022-02-27 08:53:30 +00:00
const query = useQuery(req)
2022-04-05 13:56:16 +00:00
const body = await useBody(req) // only for POST | PUT | PATCH | DELETE requests
2022-01-17 10:56:12 +00:00
const cookies = useCookies(req)
return { query, body, cookies }
}
```
Learn more about [h3 methods ](https://www.jsdocs.io/package/h3#package-index-functions ).
2021-10-11 12:57:54 +00:00
## Server Middleware
Nuxt will automatically read in any files in the `~/server/middleware` to create server middleware for your project.
2021-12-23 13:52:03 +00:00
These files will be run on every request, unlike [API routes ](#api-routes ) that are mapped to their own routes. This is typically so you can add a common header to all responses, log responses or modify the incoming request object for later use in the request chain.
2021-10-11 12:57:54 +00:00
Each file should export a default function that will handle a request.
```js
export default async (req, res) => {
req.someValue = true
}
```
There is nothing different about the `req` /`res` objects, so typing them is straightforward.
```ts
import type { IncomingMessage, ServerResponse } from 'http'
export default async (req: IncomingMessage, res: ServerResponse) => {
req.someValue = true
}
```
2021-12-06 10:28:17 +00:00
2022-03-16 11:16:05 +00:00
To pass the request deeper into the application, you can `return` inside the function:
2022-03-09 13:42:42 +00:00
```js
export default async (req, res) => {
const isNotHandledByThisMiddleware = req.url.includes('/some-unhandled-url-path/')
if(isNotHandledByThisMiddleware) {
return
}
// Actual logic here
}
```
2022-03-16 11:16:05 +00:00
::alert{type=info icon=🔎}
Find more information about custom middleware in the documentation for [nuxt.config.js ](/docs/directory-structure/nuxt.config#servermiddleware )
::