Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@e22m4u/js-trie-router

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@e22m4u/js-trie-router

Trie-based router for Node.js

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
146
decreased by-11.52%
Maintainers
0
Weekly downloads
 
Created
Source

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

typecontent-type
stringtext/plain
numberapplication/json
booleanapplication/json
objectapplication/json
Bufferapplication/octet-stream
Streamapplication/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

Keywords

FAQs

Package last updated on 04 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc