Route to proxy requets #2349
-
Would it be simple to have a route that proxies the request to a local server on another port? The idea would be to unify the interface. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Are you having trouble getting this to work, or are you looking for a more convenient way to set this up? |
Beta Was this translation helpful? Give feedback.
-
I was not sure how to set it up. Should it be a custom server? The web
server routes require an action, should it be implemented as an action ?
…On Mon, Dec 5, 2022 at 6:48 PM Evan Tahler ***@***.***> wrote:
Are you having trouble getting this to work, or are you looking for a more
convenient way to set this up?
—
Reply to this email directly, view it on GitHub
<#2349 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAD3MYUSPLILE354WVY75LWLZ5MPANCNFSM6AAAAAASU3JRUQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yep! You'll want to make an action that only works for HTTP/web servers. I like the http-proxy package to handle the details: import { Action, ParamsFrom, Connection } from "actionhero";
import { IncomingMessage, ServerResponse } from "http";
import * as httpProxy from "http-proxy"; // npm install --save --exact http-proxy @types/http-proxy
const PROXY_URL = "https://www.google.com";
const proxy = httpProxy.createProxyServer({});
export class ProxyAction extends Action {
constructor() {
super();
this.name = "proxy";
this.description = "I Proxy web requests";
this.inputs = {};
this.blockedConnectionTypes = ["websocket"]; // be sure to block all connection types (servers) other than 'web'
}
async run(data: {
params: ParamsFrom<ProxyAction>;
connection: Connection;
toRender: boolean;
}) {
const { req, res }: { req: IncomingMessage; res: ServerResponse } =
data.connection.rawConnection;
await new Promise((success, failure) => {
proxy.web(req, res, { target: PROXY_URL }, (error) => {
if (error) {
return failure(error);
}
data.toRender = false; // prevent actionhero from responding to this connection if the proxy was successful
return success(null);
});
});
}
} |
Beta Was this translation helpful? Give feedback.
Yep! You'll want to make an action that only works for HTTP/web servers. I like the http-proxy package to handle the details: