find-my-way-ts Fast HTTP Router

0.1.6 · active · verified Tue Apr 21

find-my-way-ts is a highly performant, framework-independent HTTP router designed for Node.js environments, leveraging a Radix Tree (also known as a compact Prefix Tree) for extremely fast route matching. It is a TypeScript-first fork of the popular `find-my-way` router, providing enhanced type safety and improved developer experience through its explicit type definitions. Currently at version 0.1.6, the package appears to be under active development, with frequent patch releases addressing bug fixes and minor enhancements, such as improved URI error handling and `QueryString` module integration. Its primary differentiator is its speed and efficient algorithm for path resolution, supporting route parameters and wildcards, making it suitable for high-throughput HTTP servers. Its pre-1.0 status suggests a relatively fast-moving API, though the current changes are mostly iterative improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic HTTP server using `find-my-way-ts` to route incoming requests. It demonstrates GET routes with and without parameters, a POST/PUT route with body parsing, and accessing query parameters. It uses Node.js's native `http` module for server creation.

import { findMyWay } from 'find-my-way-ts';
import * as http from 'http';

const router = findMyWay();

router.on('GET', '/', (req, res, params, store, searchParams) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ message: 'Hello from root!', params, searchParams }));
});

router.on('GET', '/user/:id', (req, res, params, store, searchParams) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ message: `Hello user ${params.id}!`, params, searchParams }));
});

router.on(['POST', 'PUT'], '/data', (req, res, params, store, searchParams) => {
  let body = '';
  req.on('data', chunk => { body += chunk; });
  req.on('end', () => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({ message: `Data received for ${req.method}!`, body: JSON.parse(body || '{}'), params, searchParams }));
  });
});

router.on('GET', '/search', (req, res, params, store, searchParams) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ message: 'Search results!', params, searchParams, query: req.url?.split('?')[1] }));
});

const server = http.createServer((req, res) => {
  router.lookup(req, res);
});

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server listening on http://localhost:${PORT}`);
  console.log('Try visiting:');
  console.log(`- http://localhost:${PORT}/`);
  console.log(`- http://localhost:${PORT}/user/123`);
  console.log(`- http://localhost:${PORT}/search?q=test&page=1`);
  console.log(`- curl -X POST -H "Content-Type: application/json" -d '{"item": "new"}' http://localhost:${PORT}/data`);
});

view raw JSON →