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-earth

v0.0.6

Published

Middlware manager for express.js

Downloads

495

Readme

Middle Earth

Middleware manager for Express.js

Why...?

Core objective is to keep configurations for different enviroments/use cases in one spot vs. spreading them out based on where they need to be loaded.

Install

npm install middle-earth

Usage

Examples below are basic. But should provide you with an idea of what you can do.


var express = require('express');
var MiddleEarth = require('middle-earth');

var app = express();

Appending and/or prepending.

app
  .middlewares([
    {name: 'body-parser', cb: bodyParser()},
    {name: 'method-override', cb: methodOverride()}
  ]);

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app
    .middlewares()
    .prepend([
      {name: 'basicAuth', cb: express.basicAuth("user", "strong")}
    ])
    .append([
      {name: 'something-else', cb: somethingElse()},
      {name: 'and-another', cb: andAnother()}
    ]);
}

app.middlewares().finish();

Equivalent to:

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use(express.basicAuth("user", "strong"));
}

app.use(bodyParser());
app.use(methodOverride());

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use(somethingElse());
  app.use(andAnother());
}

You can also insert a middleware before or after another.

app
  .middlewares([
    {name: 'body-parser', cb: bodyParser()},
    {name: 'method-override', cb: methodOverride()}
  ]);

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app
    .middlewares()
    .before('body-parser', {name: 'basicAuth', cb: express.basicAuth("user", "strong")});
    .after('body-parser', {name: 'other', cb: other()});
}

app.middlewares().finish();

Equivalent to:

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use(express.basicAuth("user", "strong"));
}

app.use(bodyParser());
app.use(methodOverride());

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use(other());
}

Pathed middlewares

app
  .middlewares([
    {name: 'body-parser', cb: bodyParser()},
    {name: 'method-override', cb: methodOverride()},
    {name: 'static', cb: express.static(__dirname+'/../public')}
  ])

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app
    .middlewares()
    .prepend([
      {name: "basic-auth", cb: express.basicAuth("user", "strong"), path: "/admin"}
    ])
    .append([
      {name: 'other', cb: other()}
    ]);
}

app.middlewares().finish();

Equivalent to:

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use("/admin", express.basicAuth("user", "strong"));
}

app.use(bodyParser());
app.use(methodOverride());
app.use(express.static(__dirname+'/../public'));

if (['production', 'staging'].indexOf(process.env.NODE_ENV) >= 0) {
  app.use(other());
}

Execute a function at a specific point. Primary use would be to allow routes to be mapped at a particular spot.

var route = express.Router();
route.get("/posts", function(req, res, next) {
  res.send(200);
});

app
  .middlewares([
    {name: 'compress', cb: compress()},
    {name: 'logger', cb: Log.logger('dev')},
    {name: 'body-parser', cb: bodyParser()},
    {name: 'method-override', cb: methodOverride()},
    {name: 'cookie-parser', cb: cookieParser('secret')},
    {name: 'session', cb: session({secret: 'secret', key: 'sid', cookie: {secure: true}})},
    {name: 'csrf', cb: Csrf.csrf()},
    {name: 'csrf-local-token', cb: Csrf.localToken()},
    {name: 'static', cb: express.static(__dirname+'/../public')}
  ]);

app
  .middlewares()
  .before('static', {name: 'routes', fn: function() {
    app.use(route);
  }});

app.middlewares().finish();

Equivalent to:

app.use(compress());
app.use(Log.logger('dev'));
app.use(bodyParser());
app.use(methodOverride());
app.use(cookieParser('secret'));
app.use(session({secret: 'secret', key: 'sid', cookie: {secure: true}}));
app.use(Csrf.csrf());
app.use(Csrf.localToken());
app.use(route);
app.use(express.static(__dirname+'/../public'));

Removing a middleware from the queue.

app
  .middlewares([
    {name: 'body-parser', cb: bodyParser()},
    {name: 'method-override', cb: methodOverride()},
    {name: 'static', cb: express.static(__dirname+'/../public')},
    {name: 'logger', cb: logger()}
  ])

if ('test' === process.env.NODE_ENV) {
  app
    .middlewares()
    .remove('logger');
}

app.middlewares().finish();

Equivalent to:

app.use(bodyParser());
app.use(methodOverride());
app.use(express.static(__dirname+'/../public'));

if ('test' != process.env.NODE_ENV) {
  app.use(logger());
}

Ovewrite an existing middleware in the queue.

app
  .middlewares([
    {name: 'overwrite-me', cb: fn1()},
    {name: 'no-touchy', cb: fn2()}
  ]);

if ('test' === process.env.NODE_ENV) {
  app
    .middelwares()
    .overwrite('overwrite-me', {name: 'diff-name', cb: fn3()});
}

app.middelwares().finish();

Equivalent to:

if ('test' === process.env.NODE_ENV) {
  app.use(fn3());
} else {
  app.use(fn1());
}

app.use(fn2());

You may also just pass it a middlware/function.

  app
    .middelwares()
    .overwrite('overwrite-me', function(req, res, next) {
      next();
    });

Note, make sure you don't confuse your "middlewares" with the "function" executables you can add to the queue.

Important

This does not alter middlewares already applied (use'd) on the app.

You can use it in conjuction with the normal app.use(), but middlewares will be applied when you call #finish() and the order will be set accordingly to that invocation point.

Test

npm test

License

MIT