
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
fastify-http-proxy
Advanced tools
Proxy your http requests to another server, with hooks.
This fastify
plugin forward all the request
received with a given prefix (or none) to an upstream. All Fastify hooks are still applied.
fastify-http-proxy
is built on top of
fastify-reply-from
, which enables
you for single route proxying.
This plugin can be used in a variety of circumstances, for example if you have to proxy an internal domain to an external domain (useful to avoid CORS problems) or to implement your own API gateway for a microservices architecture.
Fastify 2.x. See this branch and related versions for Fastify 1.x compatibility.
npm i fastify-http-proxy fastify
const Fastify = require('fastify')
const server = Fastify()
server.register(require('fastify-http-proxy'), {
upstream: 'http://my-api.example.com',
prefix: '/api', // optional
http2: false // optional
})
server.listen(3000)
This will proxy any request starting with /api
to http://my-api.example.com
. For instance http://localhost:3000/api/users
will be proxied to http://my-api.example.com/users
.
If you want to have different proxies on different prefixes in you can register multiple instances of the plugin as shown in the following snippet:
const Fastify = require('fastify')
const server = Fastify()
const proxy = require('fastify-http-proxy')
server.register(proxy, {
upstream: 'http://my-api.example.com',
prefix: '/api', // optional
http2: false // optional
})
server.register(proxy, {
upstream: 'http://single-signon.example.com/auth',
prefix: '/auth', // optional
http2: false // optional
})
server.listen(3000)
Notice that in this case it is important to use the prefix
option to tell the proxy how to properly route the requests across different upstreams.
For other examples, see example.js
.
This fastify
plugin supports all the options of
fastify-reply-from
plus the following.
Note that this plugin is fully encapsulated, and non-JSON payloads will be streamed directly to the destination.
An URL (including protocol) that represents the target server to use for proxying.
The prefix to mount this plugin on. All the requests to the current server starting with the given prefix will be proxied to the provided upstream.
The prefix will be removed from the URL when forwarding the HTTP request.
Rewrite the prefix to the specified string. Default: ''
.
A preHandler
to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
Object with reply options for fastify-reply-from
.
The following benchmarks where generated on a Macbook 2018 with i5 and 8GB of RAM:
Framework | req/sec |
---|---|
express-http-proxy | 878.4 |
http-proxy | 3837 |
fastify-http-proxy | 4205 |
fastify-http-proxy (with undici ) | 6235.6 |
The results where gathered on the second run of autocannon -c 100 -d 5 URL
.
MIT
FAQs
`fastify-http-proxy@6.3.0` has been deprecated. Please use `@fastify/http-proxy@7.0.0` instead.
The npm package fastify-http-proxy receives a total of 1,575 weekly downloads. As such, fastify-http-proxy popularity was classified as popular.
We found that fastify-http-proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 17 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.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.