Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resp-modifier

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resp-modifier

Middleware for modifying a HTML response

  • 6.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1M
increased by60.2%
Maintainers
1
Weekly downloads
 
Created

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

FAQs

Package last updated on 29 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc