Nuxt/docs/content/3.server/1.api.md
Daniel Roe c75c03e0bb
docs: add deployment (#211)
Co-authored-by: Alexander Lichter <manniL@gmx.net>
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
2021-06-14 14:31:30 +02:00

813 B

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(). API routes are powered by h3.

Example: Hello world

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

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

Example: An async function

import express from 'express'

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

  return {
    someData: true
  }
}

Example: Using Node.js style

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