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

@exact-realty/routemate

v1.0.5

Published

Routemate is a JavaScript router with support for various environments such as Node.js, Deno and Cloudflare Workers, with more to come.

Downloads

22

Readme

Routemate

Routemate is a JavaScript router with support for various environments such as Node.js, Deno and Cloudflare Workers, with more to come. It is configured similarly to Express.js but uses standard Request, Response, and Headers elements.

Reliability Rating Vulnerabilities Bugs Security Rating Maintainability Rating NPM Downloads

Installation

You can install Routemate via npm or yarn:

npm install "@exact-realty/routemate"
yarn add "@exact-realty/routemate"

Getting Started

Here's an example of setting up Routemate for Node.js:

import server, { listeners } from '@exact-realty/routemate';
import nodeListener from './node';
// Optional error handler
import { handleResponseError } from 'routemate/dist/ResponseError';

// Set up router with Node.js bindings
const router = server(listeners.node);
const port = 3000;
const host = 'localhost';

router.get('/', (_req, res) => {
  const responseBody = 'Hello, World';
  const headers = { 'content-type': 'text/plain' };
  return new Response(responseBody, { headers });
});

router['use:error'](handleResponseError);

API

Router

Router is the base router method. It provides several methods to set up routing like .use and.route (['use:error'] and ['route:error'] can be used for setting up error handlers). It also provides convenience methods for the standard HTTP methods, like .get, .head, .post, etc.

import { Router } from '@exact-realty/routemate';

const r = Router();

// Request handlers receive three arguments:
// * req: A Request object with the original request
// * res: The latest response in the pipeline.
//        This can be a Response object, `undefined`
//        for first handler in the pipeline, or it
//        can be what the last handler returned.
// * url: A URL object with the request URL for convenience
// Handlers should return a THandlerResponse, defined as follows:
// type TResponse = Response | number | null | undefined;
// Handlers may throw a Response object or a number.
// This has the effect of returning said response, skipping
// the remaining handlers in the pipeline.

// Error handlers are also evaluated in a pipeline and receive
// four arguments
// * err: The value thrown in a request handler
// * req: As for request handlers
// * res: As for request handlers
// * url: As for request handlers

// Handlers can return a number for an empty response with that
// status code
r.get('example/foo', () => 200);
// Handlers can be async
r.post('example/foo', () => Promise.resolve(201));
// A null response is an empty 204 No Content response
r.put('example/foo', () => null);
// An undefined response corresponds to 501 Not Implemented
r.patch('example/foo', () => undefined);
// .route allows for custom HTTP methods. Methods can be a string
// or an array
r.route(['TEST'], 'example/foo', () => 404);
// Paths can be regular expressions
r.use(/^example\/bar$/, () => new Response(null, { status: 599 }));

// Multiple handlers for the same path.
// They are evaluated *in order*, but the last one is the final
// response. Handlers receive the previous response as their second
// argument, so this can be helpful for setting up pipelines
r.get('example/qux', () => 400);
r.get('example/qux', () => 202);

// Routers can be nested
const child = Router();
const grandchild = Router();

child.get('example/bar', () => 201);
child.get('example/baz', grandchild);
grandchild.route(undefined, undefined, () => 202);

server

The main entry point for Routemate is the server. A server is similar to a Router, except that it also provides a listen method. The listen method is used to accept connections.

The .listen method returns a Promise that evaluates to a Router when successful. It takes three optional arguments, a port number, a hostname and an AbortSignal instance. Not all listeners support all arguments. Some listeners may require certain arguments (like a port number) to be given.

import server, { listeners } from '@exact-realty/routemate';

const app = server(listeners.node);

// Optional arguments
const port = 3000;
const host = 'www.example.com';
const abortController = new AbortController();

app.get('/', () => 200);

const router = await app.listen(port, host, abortController.signal);

// Continue configuration
router.get('/route', () => 200);

// Shut down the server after one second
setTimeout(() => abortController.abort(), 1000);

Roadmap

  • Add support for more environments

Contributing

Contributions are welcome! If you have any ideas for improving this router, please open an issue or submit a pull request.

License

This router is licensed under the ISC License. See the LICENSE file for details.