Nuxt/docs/content/3.server/2.api.md
Daniel Roe 052edd220d
docs: auto-generate configuration api (#349)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
2021-08-11 23:48:33 +02:00

838 B

navigation
title
API

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()).

Examples

Hello world

export default (req, res) => 'Hello World'

See result on http://localhost:3000/api/hello

Async function

export default async (req, res) => {
  await someAsyncFunction()

  return {
    someData: true
  }
}

Example: Using Node.js style

import type { IncomingMessage, ServerResponse } from 'http'

export default async (req: IncomingMessage, res: ServerResponse) => {
  res.statusCode = 200
  res.end('Works!')
}