Astro front matter is like a single function call that takes a request and returns a rendered template. Since it’s a function call you can handle the request, fetch data to be fed into the template, and exit early (as with the redirect) if needed.
API Routes
Astro’s HTML-first approach to web development will get you pretty far on its own, but Astro also supports interactive UI using your favorite framework (like React). When you build for the client, you often need endpoints to read and write data between the user and the server. Built-in API routes are here to deliver that need.
In Astro, an API route is a .js or .ts file in the src/pages/ folder, that takes a request and returns a response. API routes were designed for maximum flexibility:
- Build a form submission handler for JS-free form submission.
- Build an upload handler for user file submissions.
- Build a JSON-based REST API for the client to talk to.
- Build a dynamic asset route to return any file type, including images and videos.
API routes work by exporting a function that implements an HTTP method. Here is an example API route that saves a user profile:
export async function post(params, request) { const profile = await request.json() await saveProfile(profile) return new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "Content-Type": "application/json", }, })}Adapters: Deploy Astro Anywhere
Modern web hosts set a high bar for developer experience. Developers expect frameworks to integrate with their favored providers, without too much configuration or setup.
When we built Astro SSR, we evaluated the different ways that other frameworks tackle this problem. In the end, we decided to follow in the footsteps of SvelteKit and Remix and adopt the idea of pluggable host adapters. Adapters are simple, pluggable integrations that automatically configure your build for your favorite host.
Not only is the adapter model easy for developers to set up, but it also lets us support as many different kinds of hosts as possible, including:
- Run entirely on the edge with Cloudflare and Deno Deploy.
- Run on a modern dev platform like Netlify and Vercel.
- Run on a bare-metal serverless function on AWS, Azure, and Google Cloud.
- Run on a JavaScript server runtime can deploy yourself like Node.js and Deno.
SSR support in Astro is still experimental. Over the next two months, we’re excited to work with all of the major hosting providers to launch more adapters and partnerships for every platform that our users care about. If you are a hosting provider interested in building your own Adapter for Astro, please reach out to us on Discord or via email: [email protected].
To celebrate this announcement, we were lucky enough to work with our launch partner (and official hosting sponsor) Netlify, to launch an official, day-one adapter for the Netlify platform. The Netlify adapter configures your Astro SSR build to run on Netlify Functions with just one line of code: