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

generic-middleware

v2.1.0

Published

Express-like generic middleware that you can use for anything.

Downloads

177

Readme

generic-middleware

Express-like generic middleware that you can use for anything.

build status

Example

var middleware = require('generic-middleware');
var http = require('http');
var app = middleware();

app.use(function logRequest(req, res, next) {
  console.log('%s %s', req.method, req.url);

  if (req.url === '/') {
    return setTimeout(function() { next(); }, 5000);
  }

  next(new Error('Page Not Found'));
});

app.use(function stringifyHeaders(req, res, next) {
  res.end(JSON.stringify(req.headers, null, 2));
});

// error handler auto-detection
// must use the same arguments as previous middleware and `err`
app.use(function handlerErrors(err, req, res, next) {
  if (/not found/ig.test(err.message)) {
    res.statusCode = 404;
    res.end('Page ' + req.url + ' was not found.');
  } else {
    res.statusCode = 500;
    res.end('something bad happened');
  }
});

http.createServer(app).listen(7777);
console.log('now visit http://localhost:7777');

For more examples checkout the examples/ folder.

API

Middleware()

  • @constructor

Exported by "generic-middleware" module

Middleware#use(fn)

  • @param {Function} fn - function to inject into the stack

Middleware#useAfter(after, fn)

  • @param {Function?} [after] - function in the stack to follow (if null, add to the tail)
  • @param {Function} fn - function to inject into the stack

Middleware#useBefore(before, fn)

  • @param {Function?} [before] - function in the stack to precede (if null, add to the head)
  • @param {Function} fn - function to inject into the stack

Middleware#setParams(..args)

  • @param {..String|[String]} - either specify the params directly as function arguments or pass in an array (of strings)

How does it work?

You attach middleware by using app.use(), which has the same signature as Connect middleware (param1, param2, ... callback). When app() is called, its arguments will be remembered and passed onto the middleware functions, along with a callback that is used to determine when the next middleware should be called.

The error handling logic looks at the number of function parameters used by app.use() to compare the newly added function to the 1st one added to the stack, and if the number of params if higher then it must be an error handler (think of app.use(function(a, b, c) { /*...*/ }) vs app.use(function(err, a, b, c) { /*...*/ })). What that means if that you shouldn't omit params even if you don't use them.

For more info checkout the tests and examples.

FAQ

  • When should I use .useAfter() or .useBefore()?

When you are releasing an app to be used by others and make some middleware publically accessible, so that others can hook their custom stuff before and after a specific middleware function from your stack.

  • My error handler is not being called, why is that?

Make sure you are defining the error handling function with the same arguments as the regular middleware plus the error param (as the 1st one).

  • Can I use this module on the frontend as well?

Sure thing, it's really lightweight and has no external dependencies so far either.

License

MIT