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

cloudflare-router

v3.0.0-beta8

Published

An express-like router for Cloudflare Workers - created with TypeScript.

Downloads

49

Readme

Cloudflare-Router

A library for easily processing incoming requests to Cloudflare Workers. Created with TypeScript!


NPM CircleCI codecov Codacy Badge FOSSA Status Dependencies Status


This module is super-easy to use, and it's plug-and-play. Literally. All you have to do to start is to tell the module when you want to process a request, and it will handle everything for you.

Installing

Simply enter the following command into your terminal:

npm install cloudflare-router

alternatively

yarn add cloudflare-router

Here's a short example on how to use cloudflare-router!

import { Router } from "cloudflare-router";


const router = new Router();
const api = new Router();

router.use("/api", api);

// -> your-website.com/api/time
api.get("/time", (req, res) => {
    return res
        .statusCode(200)
        .json({
            success: true,
            time: new Date().toISOString()
        });
});

// -> your-website.com/
router.get("/", (req, res) => {
    return res.text("Welcome to my worker!");
});

// -> your-website.com/greet/Martin
router.get("/greet/:name", (req, res) => {
    return res.text(`Hello there, ${ req.matchedParams.name }`);
});


// Connecting it to the "fetch" event
addEventListener("fetch", event => {
    return event.respondWith(
        router.serveRequest(event.request, {/* extra data */ })
            .then(built => built.response)
    );
});

Middlewares and API

Cloudflare-router had the goal to be very similar to express in action, but there are still minor differences.

Using middlewares - the API

const router = new Router();

// This middleware will be called for every request to your-website.com/
// but just that path. 
router.use((req, res, next) => {
    res.locals.middlewareUsed = true;
    
    // If you want it to end at this middleware (no further processing)
    // change it to next(false). For example if auth failed.
    next();
});

/*
    When the above middleware will execute:
    ✔ your-website.com/
    ❌ your-website.com/hi
    ❌ your-website.com/hi/there
 */

// To make it run on every path (and sub-paths, if you can call it that)
// you need to add the path pattern, like this:
router.use("/*", (req, res, next) => {
    res.locals.middlewareUsed = true;
    
    next();
});

/*
    When the above middleware will execute:
    ✔ your-website.com/
    ✔ your-website.com/hi
    ✔ your-website.com/hi/there
 */

An example of real-world use

Checking that a request has a valid auth key before processing further

router.use("/api*", (req, res, next) => {
    const isAuthKeyValid = false;
    
    if (!isAuthKeyValid) {
        res
            .statusCode(401)
            .json({
                success: false,
                message: "Invalid auth key!"
            });
        
        // This will stop the request processing at this point
        return next(false);
    }
    
    // Otherwise, we'll let them pass!
    next();
});

router.get("/api/", (req, res) => {
    return res
        .statusCode(200)
        .json({
            success: true,
            message: "You have access to the API!"
        });
});

FOSSA Status