@astro-utils/express-endpoints
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

Astro Express Endpoints

Let you use an express-like framework in astro endpoints, with builtin:

Usage

const router = new ExpressRoute();

router.validate({
    body: z.object({
        name: z.string()
    })
});

export const POST = router.route(async (req, res) => {
    await new Promise(res => setTimeout(res, 1000));
    res.json({
        name: req.body.name,
        url: 'This is a POST request'
    });
});

When using the validate method it will be applied only to the next route.

Meaning that you can use the same router for multiple methods.

Body parser

The default body-parser is auto meaning that it will parse the body no matter the type of it including multipart/form-data.

You can configure the body parser by calling the body method.

const router = new ExpressRoute();

router.body('multipart');

export const POST = router.route((req, res) => {
    const myFile = req.filesOne.myFile;

    res.json({
        name: myFile?.name || 'No file',
    });
});

Cookie parser

Use the default astro cookie parser for that

Package Sidebar

Install

npm i @astro-utils/express-endpoints

Weekly Downloads

31

Version

2.1.1

License

MIT

Unpacked Size

21 kB

Total Files

14

Last publish

Collaborators

  • ido.pluto