Asynchronous HTTP microservices framework with built-in body parsing and error handling
npx @tessl/cli install tessl/npm-micro@10.0.0Micro is a minimalist framework for building asynchronous HTTP microservices in Node.js. It features an ultra-lightweight architecture (~260 lines of code) with built-in support for async/await patterns, JSON parsing utilities, automatic error handling with customizable status codes, and flexible deployment options including TCP, UNIX domain sockets, and Windows named pipes.
npm install microimport { serve, send, sendError, run, buffer, text, json, createError, HttpError } from "micro";
import type { IncomingMessage, ServerResponse } from "http";For CommonJS:
const { serve, send, sendError, run, buffer, text, json, createError, HttpError } = require("micro");import { serve, send, sendError, json } from "micro";
import type { IncomingMessage, ServerResponse } from "http";
import http from "http";
// Simple microservice
const handler = async (req: IncomingMessage, res: ServerResponse) => {
return "Hello World";
};
// Programmatic usage
const server = new http.Server(serve(handler));
server.listen(3000);
// With JSON body parsing
const jsonHandler = async (req: IncomingMessage, res: ServerResponse) => {
if (req.method === "POST") {
const data = await json(req);
return { received: data, timestamp: Date.now() };
}
return { message: "Send a POST request with JSON" };
};Micro is built around several key components:
serve function converts async handlers to standard HTTP listenersbuffer, text, json) for parsing request bodies with size limitssend function with automatic content-type detection for different data typesPrimary server creation and request handling functionality. The heart of Micro's microservice architecture.
function serve(fn: RequestHandler): RequestListener;
function run(req: IncomingMessage, res: ServerResponse, fn: RequestHandler): Promise<void>;
type RequestHandler = (req: IncomingMessage, res: ServerResponse) => unknown;Request body parsing utilities for handling JSON, text, and binary data with size limits and encoding support.
function buffer(req: IncomingMessage, options?: BufferInfo): Promise<Buffer>;
function text(req: IncomingMessage, options?: BufferInfo): Promise<string>;
function json(req: IncomingMessage, options?: BufferInfo): Promise<unknown>;
interface BufferInfo {
limit?: string | number | undefined;
encoding?: BufferEncoding;
}Response sending utilities with automatic content-type detection and error response management.
function send(res: ServerResponse, code: number, obj?: unknown): void;
function sendError(req: IncomingMessage, res: ServerResponse, errorObj: Error | HttpError): void;HTTP error creation and handling with status codes and structured error responses.
class HttpError extends Error {
constructor(message: string);
statusCode?: number;
originalError?: Error;
}
function createError(code: number, message: string, original: Error): HttpError;CLI binary for running microservices with support for multiple endpoint types and configuration options.
micro [options] [entry_point.js]
Options:
--help Show help message
-v, --version Show version number
-l, --listen <uri> Specify listen URI (tcp://, unix:, pipe:)