new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Nuxt · API · all subjects

utility functions & macros

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Server utilities and custom handlers

Server routes are powered by h3js/h3 which provides helper functions. Additional helpers can be added in the server/utils directory. You can define custom handler utilities that wrap original handlers and perform additional operations before returning the final response.

Reading request body in server routes

Use readBody(event) to read the request body asynchronously in POST/PUT handlers. When using readBody within a GET request, it will throw a 405 Method Not Allowed HTTP error. Alternatively, use readValidatedBody with a schema validator like Zod or Valibot for runtime and type safety.

Query parameters in server routes

Use getQuery(event) to retrieve query parameters from the request URL. Example for /api/query?foo=bar&baz=qux: const query = getQuery(event); returns { foo: 'bar', baz: 'qux' }. Alternatively, use getValidatedQuery with a schema validator for runtime and type safety.

Setting response status codes in server routes

Use setResponseStatus(event, statusCode) to return custom status codes. Example: setResponseStatus(event, 202) returns a 202 Accepted status.

Parsing cookies in server routes

Use parseCookies(event) to retrieve and parse cookies from incoming requests in server routes. Example: const cookies = parseCookies(event);

Forwarding context and headers with event.$fetch

By default, headers from incoming requests and request context are not forwarded when making fetch requests in server routes. Use event.$fetch to forward the request context and headers when making fetch requests. Headers that are not meant to be forwarded (transfer-encoding, connection, keep-alive, upgrade, expect, host, accept) will not be included.

event.waitUntil for background tasks

Use event.waitUntil(promise) to await a promise in the background without delaying the response to the client. The promise will be awaited before the handler terminates, ensuring asynchronous tasks like caching and logging are completed without blocking the response. Example: event.waitUntil(timeConsumingTask()); return 'done';

Sending redirects from server routes

Use sendRedirect(event, '/path', statusCode) to send redirect responses. Example: export default defineEventHandler(async (event) => { await sendRedirect(event, '/path/redirect/to', 302) })

nuxt/server import surface

nuxt/server is the import surface for server utilities and what the server auto-imports resolve to. It is not tied to a particular h3 or Nitro version. Use: import { defineEventHandler } from 'nuxt/server'

Give your agent this brain