What is resp-modifier?
The resp-modifier npm package allows you to modify HTTP responses on the fly. It is particularly useful for tasks such as injecting scripts, modifying HTML content, or altering headers in HTTP responses.
What are resp-modifier's main functionalities?
Modify HTML Content
This feature allows you to modify the HTML content of the response. In this example, the title of the HTML document is changed from 'Original Title' to 'Modified Title'.
const http = require('http');
const respModifier = require('resp-modifier');
const server = http.createServer((req, res) => {
const modifier = respModifier({
modify: (req, res, body) => {
return body.replace(/<title>(.*?)<\/title>/, '<title>Modified Title</title>');
}
});
modifier(req, res, () => {
res.end('<html><head><title>Original Title</title></head><body>Hello World</body></html>');
});
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Inject Scripts
This feature allows you to inject scripts into the HTML content of the response. In this example, a script that shows an alert with the message 'Hello World' is injected before the closing </body> tag.
const http = require('http');
const respModifier = require('resp-modifier');
const server = http.createServer((req, res) => {
const modifier = respModifier({
modify: (req, res, body) => {
return body.replace('</body>', '<script>alert("Hello World");</script></body>');
}
});
modifier(req, res, () => {
res.end('<html><head><title>Test</title></head><body></body></html>');
});
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Modify Headers
This feature allows you to modify the headers of the HTTP response. In this example, a custom header 'X-Custom-Header' with the value 'ModifiedHeader' is added to the response.
const http = require('http');
const respModifier = require('resp-modifier');
const server = http.createServer((req, res) => {
const modifier = respModifier({
modify: (req, res, body) => {
res.setHeader('X-Custom-Header', 'ModifiedHeader');
return body;
}
});
modifier(req, res, () => {
res.end('<html><head><title>Test</title></head><body></body></html>');
});
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Other packages similar to resp-modifier
http-proxy-middleware
The http-proxy-middleware package allows you to create a proxy server that can intercept and modify HTTP requests and responses. It is more versatile than resp-modifier as it supports a wide range of proxy configurations and middleware options.
express-http-proxy
The express-http-proxy package is used to proxy requests in an Express application. It allows you to intercept and modify requests and responses, similar to resp-modifier, but is specifically designed to work with Express.js.
node-http-proxy
The node-http-proxy package is a full-featured HTTP proxy for Node.js. It allows you to intercept and modify HTTP requests and responses, and offers more advanced features such as load balancing and WebSocket support.
resp-modifier
All the good parts from connect-livereload without
the livereload specific bits & with multiple replacements added.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Gulp.
Release History
(Nothing yet)
License
Copyright (c) 2013 Shane Osbourne
Licensed under the MIT license.