mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
f2672ac230
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
24 lines
811 B
Markdown
24 lines
811 B
Markdown
# 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, unlike [API routes](./api) 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 use later on in the request chain.
|
||
|
||
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
|
||
}
|
||
```
|