CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-micro

Asynchronous HTTP microservices framework with built-in body parsing and error handling

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Micro

Micro 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.

Package Information

  • Package Name: micro
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install micro

Core Imports

import { 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");

Basic Usage

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" };
};

Architecture

Micro is built around several key components:

  • Request Handler System: Core serve function converts async handlers to standard HTTP listeners
  • Body Parsing Utilities: Async functions (buffer, text, json) for parsing request bodies with size limits
  • Response Management: send function with automatic content-type detection for different data types
  • Error Handling: Structured error system with HTTP status codes and development/production modes
  • CLI Interface: Binary for running microservices with flexible endpoint configuration

Capabilities

Core Server Functionality

Primary 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;

Server Functionality

Body Parsing

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;
}

Body Parsing

Response Handling

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;

Response Handling

Error Management

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;

Error Management

Command Line Interface

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:)

Command Line Interface

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/micro@10.0.x
Publish Source
CLI
Badge
tessl/npm-micro badge