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


A middleware to inject content into any HTTP response.

Build Status

Getting Started

Install the module with: npm install connect-injector


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


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


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


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


  • Fix bug not setting isIntercepted properly


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


  • Initial alpha release


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

npm loves you