Socket
Socket
Sign inDemoInstall

connectr

Package Overview
Dependencies
1
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    connectr

Connect wrapper that adds ability to insert middleware at arbitrary positions."


Version published
Weekly downloads
185
decreased by-27.73%
Maintainers
2
Install size
25.5 kB
Created
Weekly downloads
 

Readme

Source

Connectr for Connect (Node.js)

NPM version

Connectr is a layer on top of Connect that allows the insertion/removal of middlewares after the stack has been built. This is especially useful when you don't have access to the code that sets up your Connect stack (a third party module for example).

This module also supports Express.

Install

npm install connectr

Usage

var connectr = require('connectr')(app);

// you can also monkey patch app directly:
// app = require('connectr').patch(app)

// Add labeled middleware
connectr.use(middleware).as(label);

// Label middleware which is at a specific position on the stack
// This should be used only if you don't have access to the code
// that builds the stack.
//
// Tip: use `app.stack` to inspect the stack
connectr.index(index).as(label);

// Insert before middleware
connectr.before(label).use(middleware).as(label);

// Insert after middleware
connectr.after(label).use(middleware);

// Insert at beginning of stack
connectr.first().use(middleware);

// Remove middleware
connectr.remove(label);

// Assign a label to middleware already in the stack
connectr.index(i).as(label);

// the .as, .before and .after calls are optional

// have a problem? try console.log(app.stack)

Simple Example

var connect = require('connect'),
var app = connect();
var connectr = require('connectr')(app);

connectr.use(connect.cookieParser).as('cookieParser');

/* ... */

connectr.before('cookieParser').use(function (req, res, next) {
  console.log('Before cookie parser...');
  next();
}).as('log before cookie parser');

Kitchen Sink Example

var http = require('http'),
  connect = require('connect'),
  app = connect(),
  connectr = require('connectr')(app);

var cookieParser = connect.cookieParser();

// we need to manually label middlewares
cookieParser.label = 'cookieParser';

app.use(cookieParser);

connectr.before('cookieParser').use(function (req, res, next) {
  console.log('Middleware before cookie parser.');
  next();
});

connectr.after('cookieParser').use(function (req, res, next) {
  console.log('Middleware after cookie parser.');
  next();
});

// you can also use connectr to label your middlewares
// instead of labeling them manually as above

connectr.use(connect.bodyParser()).as('bodyParser');

connectr.use(function (req, res, next) {
  console.log('Last middleware');       
  res.end('Done!');
});

connectr.before('bodyParser').use(function (req, res, next) {
  console.log('Before body parser');
  next();
}).as('beforeBodyParser');

connectr.after('beforeBodyParser').use(function (req, res, next) {
  console.log('I should be called after beforeBodyParser but before bodyParser');
  next();
}).as('betweenBeforeBodyParserAndBodyParser');

connectr.after('bodyParser').use(function (req, res, next) {
  console.log('After body parser');
  next();
}).as('afterBodyParser');

//console.log(app.stack);

http.createServer(app).listen(3000);

License

MIT: http://olalonde.mit-license.org

Keywords

FAQs

Last updated on 16 Jan 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc