connect-injector
A middleware to inject content into any HTTP response.
Getting Started
Install the module with: npm install connect-injector
Injecting
The basic API looks like this:
var injector = ;var middleware = ;
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.
Examples
JSONP support
A very useful example for connect-injector is to add JSONP
support to any application/json
repsonse:
var injector = ;var inject = ; // inject needs to be used before any middleware that writes to the response;
Now any application/json
response will be wrapped into a callback if given the
callback=xyz
query parameter.
Code minification
Another use case would be to minify JavaScript files on the fly using UglifyJS:
var injector = ;var connect = ;var UglifyJS = ;// Cache for already uglified filesvar cache = {};// Function that uglifies JavaScript codevar { var toplevel = UglifyJS; toplevel; var compressor = UglifyJS; var compressed = toplevel; compressed; compressed; compressed; return compressed;}; var inject = ; // inject needs to be used before any middleware that writes to the responsevar app = ; app;
Rewriting proxied files
connect-injector is tested to work with http-proxy you can rewrite existing proxied content:
var httpProxy = ;var connect = ;var injector = ;var proxy = httpProxy;var inject = ;var { // You need to rewrite your host in order to be able to hit virtual hosts reqheadershost = 'daffl.github.io'; proxy;}; var proxyApp = ; proxyApp;
After starting the server, check http://localhost:8080/connect-injector/dummycontent.html
to see the injected content.
Release History
0.4.2
0.4.1
- Fix connect-injector to work with Node 0.10.32 (#11)
- Add debug messages and remove header rewriting (#12)
0.4.0
- Fix issue when using GZip and refactor to use Q and proper NodeJS callbacks (#9)
0.3.0
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) 2016 David Luecke
Licensed under the MIT license.