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

redirect-laundromat

v0.2.3

Published

Modular redirection resolutions middleware

Downloads

18

Readme

Redirect Laundromat

Build Status Coverage Status Code Climate NPM version

Goal

Avoid multiple redirections when resolving seo friendly urls.

Principle

  • modular resolutions through standard middleware - named washing machines (WM)
  • mimic standard middleware usage
  • expose a single middleware (possibly used as filter, configurable)

Use case example

Replace the following redirections performed by several middleware functions :

Incoming http://www.stu.ff/cn/?noise=1

  • removeSubdomain > 303 http://stu.ff/cn/?noise=1
  • setDefaultLanguage > 307 http://stu.ff/en/?noise=1
  • removeQueryParams > 301 http://stu.ff/en/

by a single redirection keeping the same middleware functions with laundromat.

Incoming http://www.stu.ff/cn/?noise=1

  • laundromat > 301 http://stu.ff/en/

Usage

Configure middleware by adding washing machines functions :


  var laundromat = new Laundromat();

  // Set laundromat _washing machines_ (that are actually standard middleware).
  //
  // On last _washing machine_ (WM) change,
  //  if new request (statusCode & url) is different from the current one,
  //  a redirection is performed,
  //  else continue to next middleware

  laundromat
    
     // add middleware functions, named _washing machines_ (WM)
     // to distinguish them from common Express middleware

     .use(function whirlpool(req, res, next){

      // some logic ... then ...

      // change nothing and go to the next WM
      return next();

      // stop WM execution stack 
      // and pass an error to the next middleware
      return next(new Error('Wow')); 

      // trigger a redirection
      //
      // `res.redirect` is actually a stubbed version of Express' method
      // that is restored after the laundromat has finished iterating
      //
      // in this case redirect changes `req`'s `url`,
      // then the laundromat WM stack is replayed
      // 
      return res.redirect(303, 'http://so.me/st/uff');

    })
    .use(laundromat.whites) // laundromat-attached washing machine
    .use(laundromat.delicates)
    .use(laundromat.wool)
  ;

Then use laundromat middleware in an Express app :

  app.use(laundromat.wash);

Schema

        --------
        | MW A |
        --------
           |
           v
    ----------------
    | Laudromat MW |--------------|
    |              |              v
                               -------- <-------
    |              |           | MW B |        |
                               -------- <---   |
    |              |              |        |   | 
                                  v        |   | (req, res) objects possibly looping
    |              |           --------    |   | when redirection triggered
                               | MW C |----|   | by MW B, C or C
    |              |           --------        | 
                                  |            |
    |              |              v            |
                               --------        |
    |              |           | MW D |--------|
                               --------
    |              |              |
    | Laudromat MW |<-------------|
    ----------------
           |
           v
        --------
        | MW E |
        --------