@mjackson/fetch-proxy
v0.2.0
Published
An HTTP proxy for the web Fetch API
Downloads
69
Readme
fetch-proxy
fetch-proxy
is an HTTP proxy for the JavaScript Fetch API.
In the context of servers, an HTTP proxy server is a server that forwards all requests it receives to another server and returns the responses it receives. When you think about it this way, a fetch
function is like a mini proxy server sitting right there in your code. You send it requests, it goes and talks to some other server, and it gives you back the response it received.
fetch-proxy
allows you to easily create fetch
functions that act as proxies to "target" servers.
Features
- Built on the standard JavaScript Fetch API
- Supports rewriting
Set-Cookie
headers received from target server - Supports
X-Forwarded-Proto
andX-Forwarded-Host
headers - Supports custom
fetch
implementations
Installation
Install from npm:
npm i @mjackson/fetch-proxy
Usage
import { createFetchProxy } from '@mjackson/fetch-proxy';
// Create a proxy that sends all requests through to remix.run
let proxy = createFetchProxy('https://remix.run');
// This fetch handler is probably running as part of your server somewhere...
function handleFetch(request: Request): Promise<Response> {
return proxy(request);
}
// Test it out by manually throwing a Request at it
let response = await handleFetch(new Request('https://shopify.com'));
let text = await response.text();
let title = text.match(/<title>([^<]+)<\/title>/)[1];
assert(title.includes('Remix'));
Related Packages
node-fetch-server
- Build HTTP servers for Node.js using the web fetch API
License
See LICENSE