npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@typeix/router

v8.7.0

Published

Node js router for typescript projects

Downloads

130

Readme

@typeix/router

Build Status Coverage Status npm

Versions from 6.x are not compatible with prior versions, router works with http, https, http2 protocols.

Router

Typeix Router is very fast lightweight wrapper around Node.js http, https, http2 servers, which supports dynamic routing, regex name capturing in route definitions, error handling on each route (global, local error handlers).

Installing:

npm i @typeix/di @typeix/router --save
npm i @types/node --save-dev

Resolved or matched route will be delivered in route handler

interface IResolvedRoute {
  params: { [key: string]: string };
  headers: { [key: string]: any };
  url: URI;
  path: string;
  method: string;
}

Router API:

class Router {
    static parseURI(path: string, headers: { [key: string]: any; }, defaultHost?: string): URI;
    addRules(rules: Array<IRouteConfig>): void;
    addRule(Class: TRoute, config?: IRouteConfig): void;
    pipe(server: Server): Server;
    get(path: string, handler: IRouteHandler): this;
    head(path: string, handler: IRouteHandler): this;
    post(path: string, handler: IRouteHandler): this;
    put(path: string, handler: IRouteHandler): this;
    delete(path: string, handler: IRouteHandler): this;
    connect(path: string, handler: IRouteHandler): this;
    options(path: string, handler: IRouteHandler): this;
    trace(path: string, handler: IRouteHandler): this;
    patch(path: string, handler: IRouteHandler): this;
    onError(path: string, handler: IRouteHandler): this;
}

Defining Routes

Creating routes can be done via REST method api or via addRules, dynamic router can be implemented via addRule but requires implementation of custom RouteRule.

import {Router, Injector} from "@typeix/router";
import {createServer, IncomingMessage, ServerResponse} from "http";
import {readFileSync} from "fs";

const router: Router = Injector.Sync.createAndResolve(Router, []).get(Router);

router.get("/", (injector: Injector, route: IResolvedRoute) => {});
router.post("/api/users", (injector: Injector, route: IResolvedRoute) => {});
router.get("/api/users", (injector: Injector, route: IResolvedRoute) => {});
router.get("/api/users/<id:(\\d+)>", (injector: Injector, route: IResolvedRoute) => {
    return {
        id: route.params.id,
        name: "Igor"
    }
});
router.patch("/api/users/<id:(\\d+)>", async (injector: Injector, route: IResolvedRoute) => {
    const request = injector.get(IncomingMessage);
    return {
        id: route.params.id,
        name: "Igor"
    }
});
router.get("/favicon.ico", (injector: Injector, route: IResolvedRoute) => {
    const response = injector.get(ServerResponse);
    response.end(readFileSync("./public/favicon.ico"));
});

Error Handling

If any errors is thrown in route handler, router will forward exception to internal route handler.

import {Router, Injector} from "@typeix/router";
import {createServer, IncomingMessage, ServerResponse} from "http";

const router: Router = Injector.Sync.createAndResolve(Router, []).get(Router);

router.get("/",  (injector: Injector, route: IResolvedRoute) => {});
router.post("/api/users",  (injector: Injector, route: IResolvedRoute) => {});
router.get("/api/users",  (injector: Injector, route: IResolvedRoute) => {});
router.get("/api/users/<id:(\\d+)>", (injector: Injector, route: IResolvedRoute) => {});
router.patch("/api/users/<id:(\\d+)>", (injector: Injector, route: IResolvedRoute) => {});

router.onError("/api/(.*)", (injector: Injector, route: IResolvedRoute) => {
    return {...route, handler: "[Function handler]", statusCode: response.statusCode};
});

router.onError("(.*)", (injector: Injector, route: IResolvedRoute) => {
    return "GLOBAL HANDLER";
});

const server = router.pipe(createServer());
server.listen(4000);

Supported Servers

Router supports all implemented servers in Node.js however http2 is implemented via http2 compatibility API `Http2ServerRequest, Http2ServerResponse

HTTP

import {Router, Injector} from "@typeix/router";
import {createServer, IncomingMessage, ServerResponse} from "http";

const router: Router = Injector.Sync.createAndResolve(Router, []).get(Router);
router.get("/", (injector: Injector, route: IResolvedRoute) => {
    const request = injector.get(IncomingMessage);
    const response = injector.get(ServerResponse);
});

const server = router.pipe(createServer());
server.listen(4000);

HTTPS

import {Router, Injector} from "@typeix/router";
import {createServer, IncomingMessage, ServerResponse} from "https";
import {readFileSync} from "fs";

const router: Router = Injector.Sync.createAndResolve(Router, []).get(Router);
router.get("/", (injector: Injector, route: IResolvedRoute) => {
    const request = injector.get(IncomingMessage);
    const response = injector.get(ServerResponse);
});

const options = {
    key: readFileSync('localhost-privkey.pem'),
    cert: readFileSync('localhost-cert.pem')
};

const server = router.pipe(createServer(options));
server.listen(4000);

HTTP2

import {Router, Injector} from "@typeix/router";
import {createServer, Http2ServerRequest, Http2ServerResponse} from "http2";
import {readFileSync} from "fs";

const router: Router = Injector.Sync.createAndResolve(Router, []).get(Router);
router.get("/", (injector: Injector, route: IResolvedRoute) => {
    const request = injector.get(Http2ServerRequest);
    const response = injector.get(Http2ServerResponse);
});

const options = {
    key: readFileSync('localhost-privkey.pem'),
    cert: readFileSync('localhost-cert.pem')
};

const server = router.pipe(createServer(options));
server.listen(4000);