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

midware

v0.1.7

Published

Module for creating connect-style middlewares. Dependency-free. Runs in the browser and node.js

Downloads

10,413

Readme

midware Build Status

midware is a tiny module to createsimple middleware layers for any node.js or browser application. Inspired in the middleware pattern by connect.

It's only ~80 SLOC.

Example

var use = require('midware')()
var message = {}

use(function(msg, next) {
  // msg === message
  next()
})

use.run(message, function(err) {
  if (err) return console.log(err)
  // finished
})

Installation

Node

To install midware in a Node application use npm.

npm install midware

Browser

bower install midware
component install h2non/midware
<script src="//cdn.rawgit.com/h2non/midware/0.1.7/midware.js"></script>

Test

To run tests use npm.

$ npm install
$ npm test

Documentation

Basic Usage

Middleware is useful for creating a plugin system or configuring anything within an application. To use midware just require it and make a call to the module.

var midware = require('midware')
var use = midware()

This will return a use function which when passed a callback will add it a waterfall sequence that will be invoked one after the other whenever the middleware is run.

use(function(next) {
  // mad science here
  next()
})

Callbacks are given a next function which will always be the last argument. Calling next will tell the middleware to call the next callback in the use sequence or will complete its run. To run the callback sequence call the method run on the use function.

use.run(function(err) {
  if (err) { return console.log(err) }
  // all done professor
})

run takes any amount of parameters that the callbacks will passed whenever run.

use(function(first, last, next) {
  console.log('Hello %s, %s', first, last)
  next()
})
use.run('Christopher', 'Turner')

Stopping

Whenever a callback should throw an exception or wish to stop the middleware from running any more calls. Give next an error or explicitly tell it stop.

use(function(next) {
  next(new Error()) // stops middleware and gives error
  next(null, true) // tells middleware to stop
})

use.run(function (err, ended) {
  // ...
})

Apply Context

Instead of binding context to callbacks, send the context to midware.

var context = {}
var use = midware(context)
use(function(next) {
  // this === context
  next()
})
use.run(function(err) {
  // this === context
})

Removing a function

You can remove registered functions in the middleware via its function name or function reference

var use = midware()
use(function test(next) {
  next()
})
use.remove('test') // by function name
var use = midware()
function test(next) {
  next()
}
use(test)
use.remove(test) // by function reference

API

midware([context])

use(callback...)

use.remove(name|function)

use.run([args...], [done])

use.flush()

use.stack = [ function... ]

License

MIT

Copyright (c) 2014 Christopher Turner Copyright (c) 2015 Tomas Aparicio