{"id":18245,"library":"connect-injector","title":"connect-injector","description":"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.","status":"maintenance","version":"0.4.4","language":"javascript","source_language":"en","source_url":"git://github.com/daffl/connect-injector","tags":["javascript","connect","middleware","http"],"install":[{"cmd":"npm install connect-injector","lang":"bash","label":"npm"},{"cmd":"yarn add connect-injector","lang":"bash","label":"yarn"},{"cmd":"pnpm add connect-injector","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used as a middleware for Connect/Express, though not a hard dependency; requires the Connect-like API","package":"connect","optional":true}],"imports":[{"note":"Package is CommonJS only; does not support ESM natively. Use require().","wrong":"import injector from 'connect-injector'","symbol":"default","correct":"var injector = require('connect-injector');"},{"note":"The module exports a single function that takes two arguments: a condition function and a converter function. Do not pass an options object.","wrong":"var inject = injector({ when: function(req, res) { return true; }, convert: function(data, req, res, callback) {} });","symbol":"injector function","correct":"var inject = injector(function(req, res) { return true; }, function(data, req, res, callback) { callback(null, data); });"},{"note":"No named exports. Use default require.","wrong":"","symbol":"None (require only)","correct":"var injector = require('connect-injector');"}],"quickstart":{"code":"var connect = require('connect');\nvar injector = require('connect-injector');\n\nvar app = connect();\n\n// Inject 'injected' into all text/html responses\nvar inject = injector(function(req, res) {\n  return res.getHeader('content-type') && res.getHeader('content-type').indexOf('text/html') === 0;\n}, function(data, req, res, callback) {\n  var injectedContent = data.toString() + '<p>injected</p>';\n  callback(null, injectedContent);\n});\n\napp.use(inject);\napp.use(function(req, res) {\n  res.setHeader('content-type', 'text/html');\n  res.end('<html><body>Hello</body></html>');\n});\n\napp.listen(3000);\nconsole.log('Server running on port 3000');","lang":"javascript","description":"Demonstrates a basic connect-injector middleware that appends a paragraph to all HTML responses."},"warnings":[{"fix":"Consider using alternatives like express-transform or write a custom middleware.","message":"Package has not been updated since 2014. No longer maintained.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Place injector middleware early in the stack, before any middleware that calls res.end() or res.write().","message":"The injector must be placed before any middleware that writes the response, otherwise it won't intercept.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always check if the header exists before using its value: res.getHeader('content-type') && res.getHeader('content-type').indexOf(...).","message":"If the response has no content-type header, condition function may crash when accessing res.getHeader('content-type').","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"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;","cause":"The condition function tries to call indexOf on undefined content-type header.","error":"TypeError: Cannot read property 'indexOf' of undefined"},{"fix":"Move injector middleware before any response-writing middleware.","cause":"The injector was placed after a middleware that already started writing or ended the response.","error":"Error: connect-injector must be used before any middleware that writes to the response"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}