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

@hapi/h2o2

Package Overview
Dependencies
Maintainers
7
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hapi/h2o2

Proxy handler plugin for hapi.js

  • 10.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
315K
decreased by-32.33%
Maintainers
7
Weekly downloads
 
Created

What is @hapi/h2o2?

@hapi/h2o2 is a plugin for the hapi.js framework that provides proxying capabilities. It allows you to forward requests to other servers, making it useful for creating APIs that aggregate data from multiple sources or for implementing reverse proxies.

What are @hapi/h2o2's main functionalities?

Basic Proxying

This code demonstrates how to set up a basic proxy using @hapi/h2o2. It forwards requests from the /proxy endpoint to example.com.

const Hapi = require('@hapi/hapi');
const H2o2 = require('@hapi/h2o2');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  await server.register(H2o2);

  server.route({
    method: 'GET',
    path: '/proxy',
    handler: {
      proxy: {
        host: 'example.com',
        protocol: 'http',
        port: 80,
        passThrough: true
      }
    }
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Proxy with Custom Headers

This example shows how to add custom headers to the proxied response using @hapi/h2o2. The onResponse function allows you to modify the response before it is sent back to the client.

const Hapi = require('@hapi/hapi');
const H2o2 = require('@hapi/h2o2');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  await server.register(H2o2);

  server.route({
    method: 'GET',
    path: '/proxy-with-headers',
    handler: {
      proxy: {
        host: 'example.com',
        protocol: 'http',
        port: 80,
        passThrough: true,
        onResponse: (err, res, request, h, settings, ttl) => {
          res.headers['x-custom-header'] = 'my-custom-value';
          return res;
        }
      }
    }
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Proxy with Query Parameters

This code demonstrates how to forward query parameters from the client request to the proxied request using the mapUri function.

const Hapi = require('@hapi/hapi');
const H2o2 = require('@hapi/h2o2');

const init = async () => {
  const server = Hapi.server({
    port: 3000,
    host: 'localhost'
  });

  await server.register(H2o2);

  server.route({
    method: 'GET',
    path: '/proxy-with-query',
    handler: {
      proxy: {
        host: 'example.com',
        protocol: 'http',
        port: 80,
        passThrough: true,
        mapUri: (request) => {
          return {
            uri: `http://example.com?${request.query}`
          };
        }
      }
    }
  });

  await server.start();
  console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {
  console.log(err);
  process.exit(1);
});

init();

Other packages similar to @hapi/h2o2

Keywords

FAQs

Package last updated on 10 Aug 2023

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