micro-map-router
v0.5.0
Published
routes for lambdas
Downloads
8
Readme
micro-map-router
Routes for lambdas.
Associate functions with the HTTP methods they should be invoked on.
Just define a Map where keys are async functions and values are arrays of allowed HTTP methods.
npm i micro-map-router
./lambda.ts
import { router } from "micro-map-router";
import { send, json } from "micro";
import { IncomingMessage, ServerResponse } from "http";
export default router(
new Map([
[
// only invoked on GET requests
async (req: IncomingMessage, res: ServerResponse) => {
send(res, 200, {});
},
["GET"]
],
[
// only invoked on POST and PUT requests
async (req: IncomingMessage, res: ServerResponse) => {
const { user } = await json(req);
send(res, 200, { user });
},
["POST", "PUT"]
]
])
);