
Astro Has a Free API You Should Know About
Astro is not just a static site generator — it has powerful server-side capabilities including API endpoints, middleware, and server-side rendering. Here's what most developers overlook. API Endpoints Astro lets you create API routes that return JSON, XML, or any response: // src/pages/api/posts.json.js export async function GET ({ request }) { const url = new URL ( request . url ) const tag = url . searchParams . get ( " tag " ) const posts = await db . post . findMany ({ where : tag ? { tags : { has : tag } } : undefined , orderBy : { publishedAt : " desc " } }) return new Response ( JSON . stringify ( posts ), { headers : { " Content-Type " : " application/json " } }) } export async function POST ({ request }) { const body = await request . json () const post = await db . post . create ({ data : body }) return new Response ( JSON . stringify ( post ), { status : 201 }) } Server-Side Rendering (SSR) With an adapter, Astro pages become server-rendered: // astro.config.mjs import { def
Continue reading on Dev.to Webdev
Opens in a new tab

