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

simple-api-router

v1.1.1

Published

A lightweight, flexible routing framework for building scalable and maintainable API infrastructures in JavaScript, with simple endpoint management and dynamic routing capabilities

Downloads

555

Readme

Simple API Router

A lightweight, flexible API routing library for JavaScript that provides a simple and extensible way to define and manage API Endpoints.

Features

  • Simple API and Endpoint class structure
  • Easy route and method matching
  • Flexible endpoint handler execution
  • Supports dynamic API routing

Usage Example

This example is taken from a Full Working Example

Defining an API

import { Api, Endpoint } from 'simple-api-router';

const API = new Api("/api/");

API.addEndpoint(new Endpoint("time", "GET", (req, res) => {
    return STATE.respondWithContent(res, Date.now().toString(), STATE.TEXT_HTML);
}));

const HTTPS_SERVER = createServerHTTPS(STATE.loadDefaultSecureContext(), (req, res) => {
    if (API.checkFindExecute(req, res)) {
        return;
    }
    
    ...
})...

Calling an API

fetch("/api/time").then((response) => response.json().then((time) => console.log(time)));

API Classes

Api Class

Constructor

  • new Api(apiRoute): Create a new API with a base route

Methods

  • addEndpoint(endpoint): Add an endpoint to the API
  • checkRoute(url): Check if a URL matches the API's base route
  • findEndpoint(endpointPath): Find an endpoint matching a given path
  • findEndpointCheckMethod(req): Find an endpoint and verify its HTTP method
  • checkFindExecute(req, res): Attempt to execute the handler for a matching endpoint

Endpoint Class

Constructor

  • new Endpoint(path, method, handler): Create a new endpoint

Methods

  • checkMethod(method): Verify the HTTP method
  • executeHandler(req, res): Execute the endpoint's handler
  • executeHandlerArgs(req, res, ...args): Execute the handler with additional arguments

Key Concepts

  • The Api class manages a collection of endpoints for a specific route prefix
  • Endpoint instances define specific path, HTTP method, and handler combinations
  • checkFindExecute method provides a convenient way to route and handle requests