Socket
Socket
Sign inDemoInstall

@fastify/http-proxy

Package Overview
Dependencies
Maintainers
20
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/http-proxy

proxy http requests, for Fastify


Version published
Weekly downloads
118K
decreased by-19.9%
Maintainers
20
Weekly downloads
 
Created

What is @fastify/http-proxy?

@fastify/http-proxy is a Fastify plugin that allows you to easily set up an HTTP proxy server. It is designed to be simple to use and integrates seamlessly with the Fastify framework, providing a way to forward requests to other servers.

What are @fastify/http-proxy's main functionalities?

Basic Proxy Setup

This code sets up a basic proxy server that forwards requests from the local server to 'http://example.com'. The 'prefix' option allows you to specify a path prefix for the proxy.

const fastify = require('fastify')();
const proxy = require('@fastify/http-proxy');

fastify.register(proxy, {
  upstream: 'http://example.com',
  prefix: '/api', // optional
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Custom Headers

This example demonstrates how to add custom headers to the proxied requests. The 'rewriteRequestHeaders' function allows you to modify the headers before they are sent to the upstream server.

const fastify = require('fastify')();
const proxy = require('@fastify/http-proxy');

fastify.register(proxy, {
  upstream: 'http://example.com',
  prefix: '/api',
  replyOptions: {
    rewriteRequestHeaders: (originalReq, headers) => {
      return {
        ...headers,
        'x-custom-header': 'my-custom-value'
      };
    }
  }
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Error Handling

This example shows how to handle errors that occur during the proxying process. The 'onError' function allows you to customize the error response sent to the client.

const fastify = require('fastify')();
const proxy = require('@fastify/http-proxy');

fastify.register(proxy, {
  upstream: 'http://example.com',
  prefix: '/api',
  replyOptions: {
    onError: (reply, error) => {
      reply.send({ error: 'Proxy error', details: error.message });
    }
  }
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/http-proxy

Keywords

FAQs

Package last updated on 01 Nov 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