GithubHelp home page GithubHelp logo

middle-man's Introduction

middle-man

Simple express middleware manager. Pull your middleware out server.js and into seperate modules. Instead of worrying about middleware execution order, focus on what dependencies exist between them, if any.

npm install --save middleman

Usage

// Require middleman
var middleman = require('middleman');

// Generate express middleware from your directory of middleware objects
var middlewareArray = middleman.generate({directory: path.resolve(__dirname, '/middleware')});
// Pass in your new, ordered middleware array into Express
app.use(middlewareArray);

Middleware Plugins

Format

module.exports = {
  name: // The name of the middleware plugin, used to reference as a dependency, etc.
  middleware: // A function or array of functions to register with your express application
  dependencies: // [Optional] An array of muddleware plugin names that must be run before this one
  priority: // [Optional] if true, order this middleware before any non-priority plugins
};

Examples

Simple Middleware

module.exports = {
  name: 'simple-plugin',
  middleware: function simplePlugin(req, res, next) {
    next();
  }
};

Middleware with Dependencies

module.exports = {
	name: 'complex-plugin',
	dependencies: ['simple-plugin'],
	middleware: [
		function middlewareA(req, res, next) {
			next();
		},
		function middlewareB(req, res, next) {
			next();
		},
		function middlewareC(req, res, next) {
			next();
		}
	]
};

Setup Middleware

module.exports = {
	name: 'setup',
	middleware: function setupPlugin(req, res, next) {
		// If you have a plugin named 'setup', it will be run first, no matter what. Do any early setup here
		next();
	}
};

middle-man's People

Contributors

fredkschott avatar vmattos avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.