Nuxt/docs/content/3.server/1.intro.md

1.9 KiB

Overview

Nuxt3 is powered by a new server engine, code-named Nitro. It has:

  • Cross-platform support for Node.js, browser, service-worker targets and more
  • Serverless support out-of-the-box
  • API routes support
  • Automatic code-splitting and async-loaded chunks
  • Hybrid mode for static + serverless sites
  • A development mode with hot module reloading

API Layer

Server API endpoints and Middleware are added by Nitro that internally uses h3.

There are a number of key features, including:

  • Handlers can directly return objects/arrays for an automatically-handled JSON response
  • Handlers can return promises, which will be awaited (res.end() and next() are also supported)
  • Helper functions for body parsing, cookie handling, redirects, headers and more

Check out the h3 docs for more information.

Direct API calls

Nitro allows 'direct' calling of routes via the globally-available $fetch helper. This will make an API call to the server if run on the browser, but will simply call the relevant function if run on the server, saving an additional API call.

$fetch API is using ohmyfetch, with key features including:

  • Automatic parsing of JSON responses (with access to raw response if needed)
  • Request body and params are automatically handled, with correct Content-Type headers being added

For more information on $fetch features, check out ohmyfetch.

Standalone Server

Nitro produces a standalone server dist that is independent of node_modules.

The server in Nuxt2 is not standalone, but requires part of nuxt core to be involved running nuxt start (with the nuxt-start or nuxt distributions) or custom programmatic usage, which was fragile and prone to breakage and not suitable for serverless and service-worker environments.