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
http-proxy-middleware
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
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
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.