Proxy
PROXYSERVER() and PROXYCLIENT().
On this page
API
method
PROXYSERVER([endpoint], [socketendpoint], [logger])
since 0.0.17
Forwards HTTP requests to a proxy client connected over a WebSocket.
PROXYSERVER([endpoint], [socketendpoint], [logger])| Parameter | Type | Description |
|---|---|---|
endpoint? |
String |
Path prefix of the requests to forward. Default: '/' |
socketendpoint? |
String |
Path of the WebSocket route for the client. Default: '/' |
logger? |
Function |
Called as logger(status, message) when the client connects or disconnects. Default: console with a date |
Registers PROXY(endpoint) for all requests except WebSocket ones and a binary WebSocket route at socketendpoint (messages up to 100 MB) for the client started with PROXYCLIENT(). Each request is sent to the client with its method, URL, headers, IP and body, and the response of the client is streamed back.
Only one client can be connected; a second one closes the first with code 4001 and is not used, so connect the client again. Without a client, requests get 400 with Not yet available.. Choose a socketendpoint that does not collide with your routes. When the request to the local app fails (the app does not run or closes the connection), the visitor gets an empty response with the status 200.
exports.install = function() {
PROXYSERVER('/', '/$proxy/');
};
method
PROXYCLIENT(proxyserver, target, [logger])
since 0.0.17
Connects a local HTTP server to a PROXYSERVER() and answers its requests.
PROXYCLIENT(proxyserver, target, [logger])| Parameter | Type | Description |
|---|---|---|
proxyserver |
String |
URL of the WebSocket endpoint of PROXYSERVER(). |
target |
String |
Local server as 'ip:port' or 'port'. |
logger? |
Function |
Called with the status and a message on connect, disconnect and each request. Default: console with a date |
Returns WebSocketClient The client connection.
Opens a binary WebSocket to proxyserver (an http/https URL is changed to ws/wss). Every request that arrives is made against the local server target with the original method, path, headers and body, plus the header x-proxy: Total.js and the host of the target. The response is streamed back.
target is 'ip:port' or only a port (then 127.0.0.1 is used). The function returns the WebSocket client; it returns nothing and prints its usage when an argument is missing.
PROXYCLIENT('https://public.example.com/$proxy/', '8000');
PROXYCLIENT('https://public.example.com/$proxy/', '192.168.1.10:8000', NOOP);