Comments (16)
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.
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.
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.
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.
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:
- NextJS Middleware: https://nextjs.org/docs/pages/building-your-application/routing/middleware
- NextJS Rewrites: https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites
from http-proxy-middleware.
Same issue here
from http-proxy-middleware.
same
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
+1
from http-proxy-middleware.
Related Issues (20)
- req.url is always '/' HOT 4
- micromatch vulnerable at v4.0.5 HOT 3
- Error emitted on proxy server after original request is aborted
- TS type compatibility HOT 4
- ERR_HTTP_HEADERS_SENT from onProxyReq with https-proxy-agent HOT 2
- Proxy to another proxy HOT 1
- Security Vulnerability in Dependency | follow-redirects HOT 2
- "Invalid context" error message is unhelpful when mixing paths with globs HOT 1
- Release plan for v3.0.0 HOT 1
- Response interceptor copyHeaders logic only removes first character of cookie domain because of non-greedy regex
- TypeError: (options.buffer || req).pipe is not a function HOT 1
- Allow custom Http Method like LIST HOT 3
- SSE close connection issue with proxy HOT 3
- V3: base path not stripped from WebSocket upgrade requests HOT 2
- SSL Error in when Upgrading from node 16 to 18 HOT 3
- Setting followRedirects to true limits the form-data body size length to 10 MB
- Removes port from target HOT 9
- Hono Support for http-proxy-middleware HOT 1
- The Cookie header is not being included in the redirected address. HOT 2
- How to reduce logger to only errors? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-proxy-middleware.