node package manager


A middleware to inject content into any HTTP response.


A middleware to inject content into any HTTP response.

Install the module with: npm install connect-injector

The basic API looks like this:

var injector = require('connect-injector');
var middleware = injector(function when(req, res) {
  // for this request and repsonse 
  // return whether or not to enable injecting 
}, function converter(content, req, res, callback) {
  content // the entire response buffer 
  req // the HTTP request 
  res // the HTTP response 
  callback // (error, data) with the injected data 

And can be used like any other Connect and Express middleware. You need to make sure to use the injector middleware before the actual content is being written.

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

var injector = require('connect-injector');
var inject = injector(function(req, res) {
  var isJSON = res.getHeader('content-type').indexOf('application/json') !== -1;
  return isJSON && req.query.callback;
}, function(data, req, res, callback) {
  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.

Another use case would be to minify JavaScript files on the fly using UglifyJS:

var injector = require('connect-injector');
var connect = require('connect');
var UglifyJS = require('uglify-js');
// Cache for already uglified files 
var cache = {};
// Function that uglifies JavaScript code 
var uglify = function (code) {
  var toplevel = UglifyJS.parse(code);
  var compressor = UglifyJS.Compressor({
    warnings: false
  var compressed = toplevel.transform(compressor);
  return compressed.print_to_string();
var inject = injector(function(req, res) {
  return res.getHeader('content-type').indexOf('application/javascript') !== -1;
}, function(data, req, res, callback) {
  // Check the cache, uglify the code if not and add it 
  if(!cache[req.url]) {
    cache[req.url] = uglify(data.toString());
  callback(null, cache[req.url]);
// inject needs to be used before any middleware that writes to the response 
var app = connect().use(inject).use(connect.static(__dirname + '/../test'));

connect-injector is tested to work with http-proxy you can rewrite existing proxied content:

var httpProxy = require('http-proxy');
var connect = require('connect');
var injector = require('connect-injector');
var proxy = httpProxy.createProxyServer();
var inject = injector(function(req, res) {
  return res.getHeader('content-type').indexOf('text/html') === 0;
}, function(data, req, res, callback) {
  callback(null, data.toString().replace('</body>', '<p>Powered by connect-injector</p></body>'));
var proxyMiddleware = function(req, res) {
  // You need to rewrite your host in order to be able to hit virtual hosts = '';
  proxy.web(req, res, {
    target: ''
var proxyApp = connect().use(inject).use(proxyMiddleware);

After starting the server, check http://localhost:8080/connect-injector/dummycontent.html to see the injected content.


  • Fix for empty responses not trying to be rewritten (#13, #14)


  • Fix connect-injector to work with Node 0.10.32 (#11)
  • Add debug messages and remove header rewriting (#12)


  • Fix issue when using GZip and refactor to use Q and proper NodeJS callbacks (#9)


  • Injector works with HTTP proxy and GZipped content (#4, #7, #8)


  • 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) 2016 David Luecke
Licensed under the MIT license.