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

route-enhancer

v1.0.6

Published

A powerful routing library for TypeScript applications.

Downloads

21

Readme

Route Enhancer

Route Enhancer is a minimalistic routing library for Node.js, designed with simplicity and extensibility in mind.

Features

  • URL pattern matching
  • URL generation for named routes
  • Middleware support
  • Parameter validation
  • Nested routing

Installation

Use the following command to install Route Enhancer:

npm install route-enhancer

Usage

Defining Routes

First, create a Router instance:

import { Router } from 'route-enhancer';

const router = new Router();

Then, add routes using the addRoute method:

import { Route } from 'route-enhancer';
import Joi from 'joi';

const handler = (req, res, next) => {
// Handle request here
};

// Define a parameter validation schema
const paramsSchema = Joi.object({
id: Joi.string().alphanum().required(),
});

const route = new Route('user', '/user/:id', handler, [], paramsSchema);

router.addRoute(route);

Nested Routes

To define nested routes, you can pass the parent route as the last parameter to the Route constructor. Here is an example:

const parentRoute = new Route('user', '/user/:id', handler, [], paramsSchema);
const childRoute = new Route('userProfile', '/profile', handler, [], undefined, parentRoute);

// Add child route to router
router.addRoute(childRoute);

// Now you can match URLs like '/user/123/profile'

Matching URLs to Routes

Use the matchUrl method to find the route that matches a given URL:

const route = router.matchUrl('/user/123');

if (route) {
// Route was found
}

Generating URLs for Named Routes

Use the generateUrl method to generate a URL for a named route:

const url = router.generateUrl('user', { id: '123' });

// url is now '/user/123'

Parameter Validation

Routes can be created with a validation schema that defines rules for their parameters. The library uses Joi for parameter validation. When a route is matched, the validateParameters method of the Route class can be used to validate the parameters against the schema:

const paramsSchema = Joi.object({
id: Joi.string().alphanum().required(),
});

const route = new Route('user', '/user/:id', handler, [], paramsSchema);

const params = { id: '123' };

try {
route.validateParameters(params);
} catch (err) {
// Parameter validation failed
}

Contributions

Contributions are welcome! Please submit a pull request or create an issue to propose changes or report bugs.

NEW FEATURES COMING SOON

  1. Dynamic Routes: The ability to add routes dynamically at runtime could be a valuable addition to the library.

  2. HTTP Method Support: The library could be extended to support HTTP methods, allowing different handlers for different methods on the same URL pattern.

  3. Wildcard and Regex Support: Extending the URL pattern matching to support wildcards and regular expressions could provide more flexibility in defining route patterns.

  4. Pre and Post Middleware: In addition to the existing middleware support, the library could provide pre and post middleware that run before and after the route handler respectively.

  5. Route Groups: A feature to group routes with a common base URL or set of middleware could simplify route management.

  6. Automatic Documentation: The library could generate API documentation based on the routes, their handlers, and their validation schemas.

  7. Route Aliases: The ability to define aliases for routes would provide additional flexibility for URL generation.

  8. Integration with popular frameworks: The library could provide seamless integration with popular Node.js frameworks like Express, Koa, etc.

  9. Rate Limiting: As a security feature, the library could support rate limiting to prevent abuse.