Asynchronous HTTP microservices framework with built-in body parsing and error handling
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core server creation and request handling functionality that forms the heart of Micro's microservice architecture.
Creates an HTTP request listener from a request handler function, enabling programmatic server creation.
/**
* Creates an HTTP request listener from a request handler function
* @param fn - Request handler function that processes requests
* @returns RequestListener compatible with Node.js http.Server
*/
function serve(fn: RequestHandler): RequestListener;
type RequestHandler = (req: IncomingMessage, res: ServerResponse) => unknown;Usage Examples:
import { serve } from "micro";
import http from "http";
// Simple microservice
const handler = async (req, res) => {
return "Hello World";
};
const server = new http.Server(serve(handler));
server.listen(3000);
// With conditional logic
const apiHandler = async (req, res) => {
if (req.url?.startsWith("/api/")) {
return { api: "response", path: req.url };
}
if (req.method === "GET") {
return "Welcome to Micro";
}
return null; // Results in 204 No Content
};Function signature for processing HTTP requests with full async/await support.
/**
* Function signature for processing HTTP requests
* @param req - Node.js IncomingMessage object
* @param res - Node.js ServerResponse object
* @returns Any value - null for 204, undefined to let handler manage response, or data to send
*/
type RequestHandler = (req: IncomingMessage, res: ServerResponse) => unknown;Executes request handlers with promise-based error handling and automatic response processing. This function is the core execution engine behind the serve function and can be used directly for custom server implementations.
/**
* Executes request handler with promise-based error handling
* @param req - Incoming HTTP request
* @param res - HTTP response object
* @param fn - Request handler function to execute
* @returns Promise that resolves when request is handled
*/
function run(req: IncomingMessage, res: ServerResponse, fn: RequestHandler): Promise<void>;Return Value Handling:
null: Sends 204 No Contentundefined: Assumes handler managed response directlyUsage Examples:
import { run } from "micro";
import http from "http";
// Direct usage for custom server implementations
const server = http.createServer(async (req, res) => {
const handler = async (req, res) => {
if (req.url === "/health") {
return { status: "ok", timestamp: Date.now() };
}
return { message: "Custom handler" };
};
await run(req, res, handler);
});
// Custom middleware-like functionality
const withLogging = (handler) => async (req, res) => {
console.log(`${req.method} ${req.url}`);
const result = await handler(req, res);
console.log(`Response sent for ${req.url}`);
return result;
};
const customServer = http.createServer(async (req, res) => {
const baseHandler = async () => ({ message: "Hello" });
await run(req, res, withLogging(baseHandler));
});