Catalog npm
Back to packageRELEASE DETAILS

3.4.2

npm

0http

Cero friction HTTP request router. The need for speed!

httprestrouterserver
Published10 Dec 2022
Direct dependencies10
Transitive dependenciesNot calculated

README

Current package documentation

Introduction

NPM version NPM Total Downloads License TypeScript support Github stars

Zero friction HTTP framework:

  • Tweaked Node.js HTTP server for high throughput.
  • High-performance and customizable request routers.

Performance Benchmarks

Check it yourself: https://web-frameworks-benchmark.netlify.app/result?f=feathersjs,0http,koa,fastify,nestjs-express,express,sails,nestjs-fastify,restana

Installation

Install the package from npm:

npm install 0http

Usage

const cero = require('0http')
const { router, server } = cero()

router.get('/hello', (req, res) => {
  res.end('Hello World!')
})

router.post('/do', (req, res) => {
  // ...
  res.statusCode = 201
  res.end()
})

//...

server.listen(3000)

Trie router (dependency-free, fastest)

A segment-trie router with the same API, middleware chaining, nested routers and error containment as the default sequential router, but no regex per route, no route cache to size, and no external dependencies. Static routes resolve in constant time and dynamic routes stay flat as the route count grows.

const trie = require('0http/lib/router/trie')
const { router, server } = cero({ router: trie() })

router.use((req, res, next) => next())
router.get('/users/:id', (req, res) => res.end(req.params.id))
router.get('/files/*', (req, res) => res.end(req.params['*']))

Options: caseSensitive (default true), ignoreTrailingSlash (default true), plus defaultRoute, errorHandler and id. Patterns: /static, /:param, /:param?, /:name.ext (or /:name.ext?), trailing /* and RegExp (named groups become params). Static segments are literal, so /a+b only matches /a+b.

Differences from the sequential router, on purpose:

  • Case-sensitive by default; caseSensitive: false folds ASCII letters only.
  • A * that is not the last segment, or a ? that is not the last character of a param segment, throws at registration instead of matching too much.
  • An absent optional param is simply not present on req.params (the sequential router sets it to undefined, which also clobbers an inherited param of the same name).
  • A trailing /* also matches the bare prefix, with '*' equal to ''.
  • Empty route segments are ignored (/a//b is /a/b); the sequential router truncates the route at the first empty segment.
  • With ignoreTrailingSlash: false, a route registered as /dir/ matches /dir/ only and /dir matches /dir only.
  • A request path that does not start with / matches nothing.
  • router.routes lists RegExp routes only; there is no cacheSize option.

Support / Donate 💚

You can support the maintenance of this project:

Security notes

  • Errors never crash the process. A route handler that throws (or rejects) after response headers are already sent is contained: the response is ended or the socket closed, and the server keeps serving. A custom errorHandler that itself throws or rejects is also contained with a bare 500. If the response has already ended, the custom errorHandler is skipped (writing to an ended response would raise an uncatchable stream error). A throwing or rejecting defaultRoute is contained the same way.
  • Mount prefixes are stripped from the path only. The query string never becomes part of a nested router's path, so GET /api/acme?x=/admin reaches the router mounted at /api/:tenant as /?x=/admin, never as /admin.
  • Sequential route matching is case-insensitive. /Admin, /admin and /ADMIN all match a route registered as /admin. Reverse proxies, WAFs and auth layers that classify paths case-sensitively must be aligned accordingly. The trie router is case-sensitive by default and treats static segments literally (no regex meaning), so it agrees with such layers out of the box.
  • Mount nested routers with use(). Only router.use(prefix, subRouter) rewrites the URL when entering a nested router. A router passed to get() or on() is treated as a plain middleware and will not match its sub-routes.
  • Nested routers own unmatched requests. If a mounted router finds no route for the rewritten URL, it responds 404 itself and the parent chain is not resumed (unlike Express mounted apps, which fall through).
  • The route cache is memory-bounded. Matches with parameters, 404s, and paths longer than 4KB are never cached, and the cache is additionally capped by total size — so attacker-controlled unique paths cannot pin memory. The trie router has no request-keyed cache at all: its static table holds one entry per registered route and known HTTP method, never per request path.

More

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 10

Native requirements
10 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
trouter
Environment
{
  "local_name": "trouter",
  "alias": false,
  "peer_metadata": null
}
^3.2.0runtime
lru-cache
Environment
{
  "local_name": "lru-cache",
  "alias": false,
  "peer_metadata": null
}
^7.14.1runtime
regexparam
Environment
{
  "local_name": "regexparam",
  "alias": false,
  "peer_metadata": null
}
^2.0.1runtime
nyc
Environment
{
  "local_name": "nyc",
  "alias": false,
  "peer_metadata": null
}
^15.1.0development
chai
Environment
{
  "local_name": "chai",
  "alias": false,
  "peer_metadata": null
}
^4.3.7development
mocha
Environment
{
  "local_name": "mocha",
  "alias": false,
  "peer_metadata": null
}
^10.1.0development
cross-env
Environment
{
  "local_name": "cross-env",
  "alias": false,
  "peer_metadata": null
}
^7.0.3development
supertest
Environment
{
  "local_name": "supertest",
  "alias": false,
  "peer_metadata": null
}
^6.3.3development
body-parser
Environment
{
  "local_name": "body-parser",
  "alias": false,
  "peer_metadata": null
}
^1.20.1development
find-my-way
Environment
{
  "local_name": "find-my-way",
  "alias": false,
  "peer_metadata": null
}
^5.2.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "ac0a8b4c71562480067201452caf8e1581acd924",
  "tarball": "https://registry.npmjs.org/0http/-/0http-3.4.2.tgz",
  "fileCount": 8,
  "integrity": "sha512-zDbvsof0hqUdtHjcXE8pCzAmBGhNjwhypDfMKQf4sJz9QB9CMphgQzo/te2bGz7GjEuqiwXss7z7DJMt7IQo3Q==",
  "signatures": [
    {
      "sig": "MEQCIDuFBgpzWf/Cwqhzrb1KsCuQNc3BsSAM2eM2LXnXuaPVAiAuGTPwIEhck/unTBji96PELzknob7LmGyIFWW3RIBrhA==",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 8577,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjlFgTACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqzqA//VlHsxKwRuFe4FBi1qWI6BP9XiFFC9o8SuH7Szi3twZNUc9R3\r\nbZJZpgmlz3kjYXS+TvM1HcrWmQl2+9MU1yt/oOJLMoZt7RFpzqG/VU7425UD\r\njOquiLcvQ8rCMLE2TNKLTIl9h/8pujucksSc0+OD0rXwgGoD/q+CcszpZ+Bu\r\nFm4M2lOgjLXC5aXe2Ehbq5O3b1mYSIuJnPH9t6NYeS0fBugbf8lQpXHtcgZ0\r\nvu13JlRuU6IB0o1nuc4OxWzvfY7UQ5l+eqyWVX+k/3PxkjDryHvLZKI2QViX\r\noYKTfDT7uLGqqg1GggqLb1PKcMxaowx9XCY0v/EQUXuARC5XePxuqtBQYw0J\r\nuzbjt0GhjFyRSsf5peZ8lelUrrhu3prtLJuAmjWYdV+D0MSSp569tgtNf0NI\r\nT/X+bGvSjQyBF+49eHIgu1k8Bps7Wy0gfgUEAqE++WEyiICpgB53dxqxmHou\r\nZKYJCFg93HwEuxSWZV6OR6Nko2bvMSUz9c1ASPa1Rzw0+jh+VPvQS0HOpIrM\r\nkTBQ168Fyx5mUymJ98mv0NxbujtlFZfCFYHjzZ5jrs17B/oxhJbgU+Y54/QK\r\nnifXz2Sf7W7y4bt9YHh/Tkz2sRcheIyKTMt6YeRK9PdDfMzyJGrNIEoNqfUA\r\ntttMuAFR8Gqkab7pWBSbEKE35jGETpy3tHU=\r\n=7k0D\r\n-----END PGP SIGNATURE-----\r\n"
}

Repository

PENDING
jkyberneees/0http

Shared repository data · GitHub

License detected in repository
Not collected

Repository

PENDING
backendstack21/0http

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 17:41 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:447f3532faa2f6a7e32d6df24da49a40ae02ec9b1d5df584b8035ccce98bed40

Other collected versions

5.2.09 Oct5.1.16 Sept5.1.021 Jun5.0.131 May5.0.014 May4.4.018 NovView all collected versions →