Nuxt/docs/content/2.concepts/2.server-engine.md

56 lines
2.8 KiB
Markdown
Raw Normal View History

# Server Engine
Nuxt 3 is powered by a new server engine, code-named "Nitro".
This engine has many benefits:
::list
- Cross-platform support for Node.js, Browsers, service-workers and more
- Serverless support out-of-the-box
- API routes support
- Automatic code-splitting and async-loaded chunks
- Hybrid mode for static + serverless sites
- Development server with hot module reloading
::
## API Layer
Server [API endpoints](/docs/directory-structure/server#api-routes) and [Middleware](/docs/directory-structure/server#server-middleware) are added by Nitro that internally uses [h3](https://github.com/unjs/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](https://github.com/unjs/h3) for more information.
::alert{type="info" icon=}
Learn more about the API layer in the [`server/`](/docs/directory-structure/server) directory.
::
## 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](https://github.com/unjs/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](https://github.com/unjs/ohmyfetch).
## Standalone Server
Nitro produces a standalone server dist that is independent of `node_modules`.
The server in Nuxt 2 is not standalone, but requires part of nuxt core to be involved running `nuxt start` (with the [`nuxt-start`](https://www.npmjs.com/package/nuxt-start) or [`nuxt`](https://www.npmjs.com/package/nuxt) distributions) or custom programmatic usage, which was fragile and prone to breakage and not suitable for serverless and service-worker environments.
This dist is generated when running `nuxt build` into a [`.output`](/docs/directory-structure/output) directory.
2021-10-13 09:12:17 +00:00
The output is combined with both runtime code to run your Nuxt server in any environment (including experimental browser Service Workers!) and serve you static files, making it a true hybrid framework for the Jamstack. In addition, a native storage layer is implemented, supporting multi source, drivers and local assets.
::alert{type="info" icon=IconCode}
Checkout the Nitro engine on GitHub: [framework/packages/nitro](https://github.com/nuxt/framework/tree/main/packages/nitro)
::