connect-injector

A middleware to inject content into any HTTP response.

npm install connect-injector
1 downloads in the last day
84 downloads in the last week
440 downloads in the last month

connect-injector

A middleware to inject content into any HTTP response.

Build Status

Getting Started

Install the module with: npm install connect-injector

Injecting

The basic API looks like this:

var inject = require('connect-injector');

inject(function when(req, res) {
  // for this request and repsonse
  // return whether or not to enable injecting
}, function converter(callback, content, req, res) {
  callback // (error, data) with the injected data
  content // the entire response buffer
});

Examples

JSONP support

A very useful example for connect-inject is to add JSONP support to any application/json repsonse:

var inject = injector(function(req, res) {
  var isJSON = res.getHeader('content-type').indexOf('application/json') !== -1;
  return isJSON && req.query.callback;
}, function(callback, data, req, res) {
  callback(null, req.query.callback + '(' + data.toString() + ')');
});

// inject needs to be used before any middleware that writes to the response
connect().use(connect.query()).use(inject).use(/* your other middleware here */);

Now any application/json response will be wrapped into a callback if given the callback=xyz query parameter.

Release History

0.2.3

  • Fix caching issues for response headers not being written if there is no body (#3)

0.2.2

  • Fix handling of responses with an empty body (#1)

0.2.1

  • Fix bug not setting isIntercepted properly

0.2.0

  • Allow chaining injectors
  • Unit tests and CI
  • Fixes for writing correct headers
  • Use stream-buffers instead of concatenating

0.1.0

  • Initial alpha release

License

Copyright (c) 2013 David Luecke
Licensed under the MIT license.

npm loves you