Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reverse proxy #1491

Closed
allmors opened this issue Sep 21, 2023 · 8 comments
Closed

Add support for reverse proxy #1491

allmors opened this issue Sep 21, 2023 · 8 comments
Labels
enhancement New feature or request.

Comments

@allmors
Copy link

allmors commented Sep 21, 2023

What is the feature you are proposing?

I hope to add support for reverse proxy. It supports functions similar to http-proxy-middilware in hono app.use. If someone can tell me how hono implements the reverse proxy function of cfworkers, it would also be great.

@allmors allmors added the enhancement New feature or request. label Sep 21, 2023
@yusukebe
Copy link
Member

yusukebe commented Sep 21, 2023

Hi @allmors

I think it is not needed. We can write a reverse proxy with this code:

import { Hono } from 'hono'

const app = new Hono()

app.all('*', async (c) => {
  const res = await fetch(c.req.raw)
  // ...
  const newResponse = new Response(res.body, res)
  // ...
  return newResponse
})

export default app

Related: #1414

@allmors
Copy link
Author

allmors commented Sep 22, 2023

Hi @allmors

I think it is not needed. We can write a reverse proxy with this code:

import { Hono } from 'hono'

const app = new Hono()

app.all('*', async (c) => {
  const res = await fetch(c.req.raw)
  // ...
  const newResponse = new Response(res.body, res)
  // ...
  return newResponse
})

export default app

Related: #1414

Thank you @yusukebe but I tested it and it caused 404 on some websites.

@yusukebe
Copy link
Member

@allmors

Which platform do you run it on? Perhaps, you don't set up an "origin". If you don't have it, you can specify a url:

app.all('*', async (c) => {
  const res = await fetch('http://example.com') // <==
  console.log(res.status)
  // ...
  const newResponse = new Response(res.body, res)
  // ...
  return newResponse
})

@allmors
Copy link
Author

allmors commented Sep 25, 2023

@allmors

Which platform do you run it on? Perhaps, you don't set up an "origin". If you don't have it, you can specify a url:

app.all('*', async (c) => {
  const res = await fetch('http://example.com') // <==
  console.log(res.status)
  // ...
  const newResponse = new Response(res.body, res)
  // ...
  return newResponse
})

I'm using hono in cloudflare workers, the following is my test code

app.all('*', async (c) => {
    const res = await fetch('https://fabfilters.online/') // <==
    console.log(c.req.raw)
    // ...
    const newResponse = new Response(res.body, res)
    // ...
    return newResponse
})

@yusukebe
Copy link
Member

For Cloudflare Workers, the following will not work without an origin.

const res = await fetch(c.req.raw)

However, if you just want to proxy, the following may be sufficient, what do you think?

const res = await fetch('https://fabfilters.online/', c.req.raw)

@allmors
Copy link
Author

allmors commented Sep 26, 2023

For Cloudflare Workers, the following will not work without an origin.

const res = await fetch(c.req.raw)

However, if you just want to proxy, the following may be sufficient, what do you think?

const res = await fetch('https://fabfilters.online/', c.req.raw)

thanks @yusukebe

@allmors allmors closed this as completed Sep 26, 2023
@kongminh113
Copy link

For Cloudflare Workers, the following will not work without an origin.

const res = await fetch(c.req.raw)

However, if you just want to proxy, the following may be sufficient, what do you think?

const res = await fetch('https://fabfilters.online/', c.req.raw)

Hi, I have tried the above code on Nodejs but it not proxy to example.com URL. Is it only work in Cloudfare ??

@yusukebe
Copy link
Member

yusukebe commented Jan 7, 2024

Hi @kongminh113 !

Thanks! It seems to be a bug! I've created the issue: honojs/node-server#121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

3 participants