HTTP маршрутизатор для Node.js на основе префиксного дерева

e22m4u 9c123b7342 chore: initial commit 1 год назад
.husky 9c123b7342 chore: initial commit 1 год назад
examples 9c123b7342 chore: initial commit 1 год назад
src 9c123b7342 chore: initial commit 1 год назад
.c8rc 9c123b7342 chore: initial commit 1 год назад
.commitlintrc 9c123b7342 chore: initial commit 1 год назад
.editorconfig 9c123b7342 chore: initial commit 1 год назад
.gitignore 9c123b7342 chore: initial commit 1 год назад
.mocharc.cjs 9c123b7342 chore: initial commit 1 год назад
.prettierrc 9c123b7342 chore: initial commit 1 год назад
LICENSE 9c123b7342 chore: initial commit 1 год назад
README.md 9c123b7342 chore: initial commit 1 год назад
eslint.config.js 9c123b7342 chore: initial commit 1 год назад
package.json 9c123b7342 chore: initial commit 1 год назад
tsconfig.json 9c123b7342 chore: initial commit 1 год назад

README.md

@e22m4u/js-trie-router

A pure ES-module of the Node.js HTTP router that uses the Trie for routing.

  • Uses path-to-regexp syntax.
  • Supports path parameters.
  • Parses JSON-body automatically.
  • Parses a query string and the Cookie header.
  • Supports preHandler and postHandler hooks.
  • Asynchronous request handler.

Installation

npm install @e22m4u/js-trie-router

Overview

A basic "Hello world." example.

import http from 'http';
import {TrieRouter} from '../src/index.js';
import {HTTP_METHOD} from '../src/route.js';

const server = new http.Server(); // A Node.js HTTP server.
const router = new TrieRouter();  // A TrieRouter instance.

router.defineRoute({
  method: HTTP_METHOD.GET,        // Request method.
  path: '/',                      // Path template like "/user/:id".
  handler(ctx) {                  // Request handler.
    return 'Hello world!';
  },
});

server.on('request', router.requestHandler);
server.listen(3000, 'localhost');

// Open in browser http://localhost:3000

RequestContext

The first parameter of the Router handler is the RequestContext instance.

  • container: ServiceContainer
  • req: IncomingMessage
  • res: ServerResponse
  • query: ParsedQuery
  • headers: ParsedHeaders
  • cookie: ParsedCookie

The RequestContext can be destructured.

router.defineRoute({
  // ...
  handler({req, res, query, headers, cookie}) {
    console.log(req);     // IncomingMessage
    console.log(res);     // ServerResponse
    console.log(query);   // {id: '10', ...}
    console.log(headers); // {'cookie': 'foo=bar', ...}
    console.log(cookie);  // {foo: 'bar', ...}
    // ...
  },
});

Sending response

Return values of the Route handler will be sent as described below.

type content-type
string text/plain
number application/json
boolean application/json
object application/json
Buffer application/octet-stream
Stream application/octet-stream

Here is an example of a JSON response.

router.defineRoute({
  // ...
  handler(ctx) {
    // sends "application/json"
    return {foo: 'bar'};
  },
});

If the ServerResponse has been sent manually, then the return value will be ignored.

router.defineRoute({
  // ...
  handler(ctx) {
    res.statusCode = 404;
    res.setHeader('content-type', 'text/plain; charset=utf-8');
    res.end('404 Not Found', 'utf-8');
  },
});

Debug

Set environment variable DEBUG=jsTrieRouter* before start.

DEBUG=jsPathTrie* npm run test

Testing

npm run test

License

MIT