-
Notifications
You must be signed in to change notification settings - Fork 852
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
Next.js App Router can't handle proxied requests #932
Comments
Same issue here. Had to work around by creating a |
I've tried handcrafting it but also failed. I created 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 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 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. |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
8 similar comments
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
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. |
Same. I wish the route segment config, e.g. |
After trying for a while I was still getting the The logic is as follow: Every time a request starting with
// 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
}
}
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:
|
I am also getting the same error. If we do this then |
Check the link above (nextjs rewrites) for the fallback option |
rewrites doesn't work for everything (e.g. websockets). |
Any update on this issue? |
Bump |
Checks
http-proxy-middleware
.Describe the bug (be clear and concise)
I'm using App Router (next 13) and trying to proxy requests on another server via createProxyMiddleware. But I've got an error by doing this. I use example from https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md#nextjs, but fitted to a new App Routes with GET and POST handlers
Error:
Step-by-step reproduction instructions
- api/internal/[...path]/route.ts
Client call:
Expected behavior (be clear and concise)
Handle proxied requests via Next 13 (App Router)
How is http-proxy-middleware used in your project?
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
The text was updated successfully, but these errors were encountered: