
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
@hapi/h2o2
Advanced tools
@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.
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();
http-proxy-middleware is a popular middleware for Node.js that provides similar proxying capabilities. It is often used with Express.js and offers a wide range of customization options, including path rewriting, custom headers, and more. Compared to @hapi/h2o2, it is more commonly used in the Express.js ecosystem.
node-http-proxy is a full-featured HTTP proxy for Node.js. It provides a robust set of features for creating proxy servers, including support for WebSockets, custom headers, and load balancing. It is a lower-level library compared to @hapi/h2o2, offering more control but requiring more setup.
express-http-proxy is a simple and configurable proxy middleware for Express.js. It allows you to forward requests to other servers with minimal configuration. It is similar to @hapi/h2o2 but is designed specifically for use with Express.js.
h2o2 is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.
FAQs
Proxy handler plugin for hapi.js
The npm package @hapi/h2o2 receives a total of 411,512 weekly downloads. As such, @hapi/h2o2 popularity was classified as popular.
We found that @hapi/h2o2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.