connect-injector
raw JSON → 0.4.4 verified Sat Apr 25 auth: no javascript maintenance
A middleware to inject content into any HTTP response, compatible with Connect and Express. Version 0.4.4 is the latest stable release (last updated in 2014). It allows conditional injection based on request/response headers and can modify response buffers on the fly. Key differentiator: intercept and transform responses before they are sent, unlike typical request-based middleware. Works with http-proxy for proxied content rewriting. Simple boolean condition function and a content converter function. No longer actively maintained; consider alternatives like express-transform or custom middleware.
Common errors
error TypeError: Cannot read property 'indexOf' of undefined ↓
cause The condition function tries to call indexOf on undefined content-type header.
fix
Check if res.getHeader('content-type') is defined before calling indexOf: return res.getHeader('content-type') && res.getHeader('content-type').indexOf('text/html') !== -1;
error Error: connect-injector must be used before any middleware that writes to the response ↓
cause The injector was placed after a middleware that already started writing or ended the response.
fix
Move injector middleware before any response-writing middleware.
Warnings
deprecated Package has not been updated since 2014. No longer maintained. ↓
fix Consider using alternatives like express-transform or write a custom middleware.
gotcha The injector must be placed before any middleware that writes the response, otherwise it won't intercept. ↓
fix Place injector middleware early in the stack, before any middleware that calls res.end() or res.write().
gotcha If the response has no content-type header, condition function may crash when accessing res.getHeader('content-type'). ↓
fix Always check if the header exists before using its value: res.getHeader('content-type') && res.getHeader('content-type').indexOf(...).
Install
npm install connect-injector yarn add connect-injector pnpm add connect-injector Imports
- default wrong
import injector from 'connect-injector'correctvar injector = require('connect-injector'); - injector function wrong
var inject = injector({ when: function(req, res) { return true; }, convert: function(data, req, res, callback) {} });correctvar inject = injector(function(req, res) { return true; }, function(data, req, res, callback) { callback(null, data); }); - None (require only)
var injector = require('connect-injector');
Quickstart
var connect = require('connect');
var injector = require('connect-injector');
var app = connect();
// Inject 'injected' into all text/html responses
var inject = injector(function(req, res) {
return res.getHeader('content-type') && res.getHeader('content-type').indexOf('text/html') === 0;
}, function(data, req, res, callback) {
var injectedContent = data.toString() + '<p>injected</p>';
callback(null, injectedContent);
});
app.use(inject);
app.use(function(req, res) {
res.setHeader('content-type', 'text/html');
res.end('<html><body>Hello</body></html>');
});
app.listen(3000);
console.log('Server running on port 3000');