0http HTTP Router

raw JSON →
4.4.0 verified Wed Apr 22 auth: no javascript

0http is a high-performance HTTP request router and server framework for Node.js, designed for high throughput and minimal overhead. Currently stable at version 4.4.0, the project maintains an active release cadence with frequent updates focused on performance, security, and Node.js compatibility. Key differentiators include its "zero friction" philosophy, highly customizable routing, and optimized Node.js HTTP server, often benchmarked among the fastest Node.js frameworks. It ships with full TypeScript type definitions since v3.5.0 and requires Node.js v22.x or higher, ensuring it leverages the latest runtime features and performance improvements.

error ERR_REQUIRE_ESM
cause Attempting to use CommonJS `require()` syntax in an ES module context when Node.js is configured for ESM.
fix
Change const cero = require('0http'); to import cero from '0http'; and ensure your package.json has "type": "module" or your file uses the .mjs extension.
error SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS context when Node.js is configured for CJS.
fix
Change import cero from '0http'; to const cero = require('0http'); or configure your project for ESM by adding "type": "module" to package.json or using .mjs file extensions.
error TypeError: cero is not a function
cause You are trying to use `cero` as a variable or object directly, but it is a factory function that needs to be called to instantiate the router and server.
fix
Ensure cero is called as a function: const { router, server } = cero();.
error TypeError: Cannot read properties of undefined (reading 'get')
cause The `router` object was not correctly destructured or instantiated from the `cero()` factory function, or `cero()` was not called at all.
fix
Verify that const { router, server } = cero(); is correctly executed and that router is in scope before attempting to define routes.
breaking Node.js v22.x or higher is now required. Older Node.js versions (below v22.x) are not supported since `0http@4.4.0` (and v20.x was required since `0http@4.2.0`).
fix Upgrade your Node.js runtime to version 22.x or later.
breaking The underlying `trouter` module was updated to v4 in `0http@4.0.0`. This may introduce breaking changes in how routes are defined, matched, or how parameters are handled.
fix Review the `trouter` v4 changelog and documentation for any necessary adjustments to your routing logic.
gotcha Versions prior to `0http@4.3.0` had potential issues with asynchronous middleware error handling, leading to unhandled exceptions or incorrect behavior.
fix Upgrade to `0http@4.3.0` or higher to benefit from improved async middleware error fixes.
gotcha Enhanced error handler security was introduced in `0http@4.4.0`. Older versions might have had less robust error handling, potentially exposing sensitive information or being vulnerable to certain attacks.
fix Upgrade to `0http@4.4.0` or higher to utilize the enhanced error handler security features.
npm install 0http
yarn add 0http
pnpm add 0http

Initializes a 0http server and router, handling a basic GET request and a POST request with body parsing, listening on port 3000.

import cero from '0http';

const { router, server } = cero();

router.get('/hello', (req, res) => {
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!');
});

router.post('/submit', (req, res) => {
  let body = '';
  req.on('data', chunk => { body += chunk; });
  req.on('end', () => {
    console.log('Received:', body);
    res.statusCode = 201;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Data received successfully!');
  });
});

server.on('error', (err) => {
  console.error('Server error:', err);
});

server.listen(3000, () => {
  console.log('0http server listening on http://localhost:3000');
});