defineEventHandler() or eventHandler() (alias).
The handler can directly return JSON data, a Promise, or use event.node.res.end() to send a response.
server/api/hello.ts
app/pages/index.vue
Server Routes
Files inside the~~/server/api are automatically prefixed with /api in their route.
To add server routes without /api prefix, put them into ~~/server/routes directory.
Example:
server/routes/hello.ts
/hello route will be accessible at http://localhost:3000/hello.
Note that currently server routes do not support the full functionality of dynamic routes as pages do.
Server Middleware
Nuxt will automatically read in any file in the~~/server/middleware to create server middleware for your project.
Middleware handlers will run on every request before any other server route to add or check headers, log requests, or extend the event’s request object.
Middleware handlers should not return anything (nor close or respond to the request) and only inspect or extend the request context or throw an error.
server/middleware/log.ts
server/middleware/auth.ts
Server Plugins
Nuxt will automatically read any files in the~~/server/plugins directory and register them as Nitro plugins. This allows extending Nitro’s runtime behavior and hooking into lifecycle events.
Example:
server/plugins/nitroPlugin.ts
Server Utilities
Server routes are powered by h3js/h3 which comes with a handy set of helpers. You can add more helpers yourself inside the~~/server/utils directory.
For example, you can define a custom handler utility that wraps the original handler and performs additional operations before returning the final response.
Example:
server/utils/handler.ts
Recipes
Route Parameters
Server routes can use dynamic parameters within brackets in the file name like/api/hello/[name].ts and be accessed via event.context.params.
server/api/hello/[name].ts
/api/hello/nuxt and get Hello, nuxt!.
Matching HTTP Method
Handle file names can be suffixed with.get, .post, .put, .delete, … to match request’s HTTP Method.
server/api/test.get.ts
server/api/test.post.ts
/test with:
- GET method: Returns
Test get handler - POST method: Returns
Test post handler - Any other method: Returns 405 error
Catch-all Route
Catch-all routes are helpful for fallback route handling. For example, creating a file named~~/server/api/foo/[...].ts will register a catch-all route for all requests that do not match any route handler, such as /api/foo/bar/baz.
server/api/foo/[...].ts
Body Handling
server/api/submit.post.ts
app/app.vue
We are using
submit.post.ts in the filename only to match requests with POST method that can accept the request body. When using readBody within a GET request, readBody will throw a 405 Method Not Allowed HTTP error.Query Parameters
Sample query/api/query?foo=bar&baz=qux
server/api/query.get.ts
Error Handling
If no errors are thrown, a status code of200 OK will be returned.
Any uncaught errors will return a 500 Internal Server Error HTTP Error.
To return other error codes, throw an exception with createError:
server/api/validation/[id].ts
Runtime Config
server/api/foo.ts
Giving the
event as argument to useRuntimeConfig is optional, but it is recommended to pass it to get the runtime config overwritten by environment variables at runtime for server routes.Request Cookies
server/api/cookies.ts