@astro-utils/express-endpoints
v3.0.2
Published
Express-like framework for Astro Endpoints
Maintainers
Readme
Astro Utils Express Endpoints
Familiar middleware. Native Astro routes.
Documentation · Examples · AI agent guide
Compose middleware, read parsed request data, and return JSON, redirects, files, or streams from Astro endpoints. An Express-style API for Astro—not a drop-in replacement for every Express application.
Install
npm install @astro-utils/express-endpoints zod@^4Validate a JSON request
src/pages/api/greet.ts
import { ExpressRoute } from '@astro-utils/express-endpoints';
import { z } from 'zod';
const router = new ExpressRoute();
router.use((_req, res, next) => {
res.set('Cache-Control', 'no-store');
next();
});
router.validate({
body: z.object({
name: z.string().trim().min(1, 'A name is required.'),
}),
});
export const POST = router.route((req, res) => {
res.json({ message: `Hello, ${req.body.name.trim()}!` });
});Send a JSON body such as {"name":"Alex"} with Content-Type: application/json to /api/greet. The route returns {"message":"Hello, Alex!"}; missing, non-string, or blank names receive a 400 response with validation errors before the handler runs.
validate() checks the body without transforming it, so the handler trims the name when using it. This example uses Zod 4, as required by the validation middleware.
A small routing toolbox
| Method | Purpose |
| --- | --- |
| use(middleware) | Add middleware. Call next() to continue, or send a response to stop. |
| body(type) | Select auto, json, multipart, urlencoded, or text parsing. |
| validate(schemas) | Add schema validation for the next registered route. |
| route(...handlers) | Create an Astro endpoint handler. |
Read parsed cookies through req.cookies; use res.cookie() to set response cookies. Forms-backed sessions are available when your application also installs and configures Forms middleware.
Getting started · Request API · Response API · Router and errors
Support Astro Utils
If Astro Utils helps you build, a star on GitHub is appreciated. Bug reports, useful examples, and pull requests help too. Thank you for supporting the project.
