@leon740727/api-server
v0.0.1
Published
Lightweight typed HTTP routing utilities for Node.js.
Maintainers
Readme
@leon740727/api-server
Lightweight HTTP routing helpers for Node.js with schema-based request parsing.
Install
pnpm add @leon740727/api-serverUsage
import { createServer } from "node:http";
import { route } from "@leon740727/api-server";
const routes = [
route.defaultCors,
route.make({
url: "/hello/{name}",
method: "GET",
handler: async ({ urlParam }) => ({
data: { message: `Hello ${urlParam?.name ?? "world"}` },
}),
}),
];
createServer((req, res) => {
route.run(req, res, routes, {}, {});
}).listen(3000);API
route.make(...): define a route with optional schema validation for headers, params, query string, and body.route.run(req, res, routes, options, resource): resolve a request and write the HTTP response.route.load(baseDir): load route modules from a directory.route.defaultCors: built-inOPTIONS *handler with permissive CORS headers.body(req): read the incoming request body into aBuffer.
Publish
pnpm run check
pnpm pack
npm publish --access public