Coder Social home page Coder Social logo

Comments (16)

liambowers avatar liambowers commented on June 7, 2024 13

If you're about to add a 'I have this issue too, +1' kind of comment, I'd suggest giving the first post a thumbs-up which communicates the same and doesn't spam subscribed users unnecessarily.

from http-proxy-middleware.

gciluffo avatar gciluffo commented on June 7, 2024 11

Same issue here. Had to work around by creating a /pages/api directory adjacent to my app directory in order to get api proxies working.

from http-proxy-middleware.

jtomaszewski avatar jtomaszewski commented on June 7, 2024 6

I've tried handcrafting it but also failed.

I created app/[...all]/router.ts file with following contents:

import httpProxy from "http-proxy";

export async function GET(request: Request) {
  const proxy: httpProxy = httpProxy.createProxy();
  const response = new Response();

  return new Promise((resolve, reject) => {
    proxy
      .once("proxyRes", resolve)
      .once("error", reject)
      .web(request, response, {
        target: "https://napiachu.pl",
        secure: false,
        changeOrigin: true,
        autoRewrite: true,
        cookieDomainRewrite: "",
      });
  });
}

But it just doesn't work, it raises TypeError: req.on is not a function error.

That's probably because the interface of request & response object is now completely different in app router (request is a node fetch "request" object, not a IncomingMessage class object coming from node http library; and response is simply not existing).

So http-proxy nor http-proxy-middleware can't be used "as is", unless we find some way how we can make it work on top of the new request & response interfaces.

from http-proxy-middleware.

karbica avatar karbica commented on June 7, 2024 2

Same. I wish the route segment config, e.g. const runtime = 'nodejs'; (the default btw), would actually pass the supported request and response interfaces. Instead we get the interfaces that would run on the edge and inside the browser which are incompatible with other third-party proxy implementations.

from http-proxy-middleware.

KakkoiDev avatar KakkoiDev commented on June 7, 2024 1

I've tried handcrafting it but also failed.

I created app/[...all]/router.ts file with following contents:

import httpProxy from "http-proxy";

export async function GET(request: Request) {
  const proxy: httpProxy = httpProxy.createProxy();
  const response = new Response();

  return new Promise((resolve, reject) => {
    proxy
      .once("proxyRes", resolve)
      .once("error", reject)
      .web(request, response, {
        target: "https://napiachu.pl",
        secure: false,
        changeOrigin: true,
        autoRewrite: true,
        cookieDomainRewrite: "",
      });
  });
}

But it just doesn't work, it raises TypeError: req.on is not a function error.

That's probably because the interface of request & response object is now completely different in app router (request is a node fetch "request" object, not a IncomingMessage class object coming from node http library; and response is simply not existing).

So http-proxy nor http-proxy-middleware can't be used "as is", unless we find some way how we can make it work on top of the new request & response interfaces.

After trying for a while I was still getting the TypeError: req.on is not a function error. So I finally dropped http-proxy-middleware in favor of a more "native" NextJS solution: with a middleware and rewrites.

The logic is as follow: Every time a request starting with /api is made, the middleware will update it as needed and then the rewrites will proxy it to the desired destination.

middleware.ts

// imports...

export async function middleware(request: NextRequest) {
  // your other middleware actions...

  if (/^\/api/.test(path)) { // if sending a request to /api/...
    const newResponse = NextResponse.next(); // prepare a new response

    // modify your response if needed
    const bearerToken = await getBearerToken();
    newResponse.headers.set("Authorization", "Bearer " + bearerToken);
    // ...

    return newResponse; // return the modified response
  }
}

next.config.js

module.exports = {
  // your other config...
  async rewrites() {
    return [
      {
        source: "/api/:path*", // get everything after /api/
        destination: `${process.env.NEXT_PUBLIC_API_URL}/:path*`, // send it to your API
      },
    ];
  },
}

For more information:

from http-proxy-middleware.

newbeea avatar newbeea commented on June 7, 2024

Same issue here

from http-proxy-middleware.

timmyg avatar timmyg commented on June 7, 2024

same

from http-proxy-middleware.

BhaskaranR avatar BhaskaranR commented on June 7, 2024

+1

from http-proxy-middleware.

stoompa avatar stoompa commented on June 7, 2024

+1

from http-proxy-middleware.

tsunamiShi avatar tsunamiShi commented on June 7, 2024

+1

from http-proxy-middleware.

aaman123 avatar aaman123 commented on June 7, 2024

+1

from http-proxy-middleware.

tomasguerreiro avatar tomasguerreiro commented on June 7, 2024

+1

from http-proxy-middleware.

childbamboo avatar childbamboo commented on June 7, 2024

+1

from http-proxy-middleware.

hmvmpire avatar hmvmpire commented on June 7, 2024

+1

from http-proxy-middleware.

dannydeut avatar dannydeut commented on June 7, 2024

+1

from http-proxy-middleware.

villqrd avatar villqrd commented on June 7, 2024

+1

from http-proxy-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.