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

middle-router

v2.2.0

Published

Route urls on both client and server through middleware

Downloads

2,463

Readme

middle-router

npm Version Greenkeeper badge Build Status

MIT License JS Standard Style

Route urls through middleware functions on both client and server.

middle-router is a universal front-end router for routing urls changes through a series of async middleware functions. This allows you to perform multiple tasks when the url changes, instead of just updating the view.

middle-router uses path-to-regexp to match paths, so it should behave much like express 4.x paths. It also uses middle-run to run the middleware series.

Usage

router.js

import Router from 'middle-router'

export default Router()
  .use(async ({ context, next }) => {
    let start = Date.now()
    await next() // start next middleware, wait for control to return
    context.totalMs = Date.now() - start
  })

  .use('/accounts', Router()
    .use('/users/:userId', async ({ params, resolve, beforeExit, exiting }) => {
      setupListeners()
      beforeExit(event => {
        if (isFormDirty) return 'Are you sure you want to leave?'
      })

      resolve(UserView({ id: params.userId })) // yields control back upstream

      await exiting // only do this after resolve, or it won't resolve until next url change!!
      cleanupListeners()
    })
  )

server-client.js

import router from './router.js'
import { Router } from 'express'
import ReactDOMServer from 'react-dom/server'

export default Router()
  .use(async (req, res, next) {
    try {
      let view = await router.route(req.url, res.locals.state)
      res.send(ReactDOMServer.renderToString(view))
    } catch (err) {
      next(err)
    }
  })

client.js

import router from './router.js'
import ReactDOM from 'react-dom'

router
  .on('route', async (args, routing) => {
    try {
      let view = await routing
      ReactDOM.render(view, document.getElementById('view'))
    } catch (error) {
      ReactDOM.render(<Error error={error}/>, document.getElementById('view'))
    }
  })
  .start()

Note: These usage examples use Express and React, and resolve each url to a React element. middle-router has no dependency on these, and can be used with whatever libraries you like.

API

Full API documentation is in the GitHub Wiki

Async Middleware

middle-router can work with any promised-based async middleware, but it was designed specifically for async functions. Inspired by koa's yield next, middle-router allows you to await next() so you can next() "downstream" and the await for control to flow back "upstream".

License

This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.