Comments (14)
I had a similar problem with front end rewriting /api
path to /
. e.g. http://www.website.com/api/whoami
would be forwarded to http://api.website.com/whoami
, but api.website.com
would set cookies with the path param not prefixed with /api
.
Solved with the following code (reads set-cookie
from incoming target response and prefixes cookie path params with /api
:
import cookiejar from 'cookiejar'
app.use('/api', proxy({
target: 'http://api.domain.com',
pathRewrite: { '^/api': '' },
onProxyRes: (proxyRes) => {
// prepend /api to cookie paths
const setCookieHeaders = proxyRes.headers['set-cookie'] || []
const modifiedSetCookieHeaders = setCookieHeaders
.map(str => new cookiejar.Cookie(str))
.map(cookie => {
if (cookie.path && cookie.path[0] === '/') {
cookie.path = `/api${cookie.path}`
}
return cookie
})
.map(cookie => cookie.toString())
proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders
},
changeOrigin: true,
}))
Would be nice if this was a configurable option for http-proxy-middleware
since it is probably a fairly common scenario.
from http-proxy-middleware.
You can try to relay all headers back and forth.
Hopefully this will take care of you cookie issue.
const proxyOptions = {
onProxyReq: relayRequestHeaders,
onProxyRes: relayResponseHeaders
}
function relayRequestHeaders(proxyReq, req) {
Object.keys(req.headers).forEach(function (key) {
proxyReq.setHeader(key, req.headers[key]);
});
}
function relayResponseHeaders(proxyRes, req, res) {
Object.keys(proxyRes.headers).forEach(function (key) {
res.append(key, proxyRes.headers[key]);
});
}
Please use StackOverflow to troubleshoot your setup if you need further assistance.
Thanks.
from http-proxy-middleware.
@chimurai : thanks for the response, it works. will use stack overflow for further information.
from http-proxy-middleware.
Good to hear it works.
Credits should go to Yomguithereal/kotatsu#106 for providing this solution.
Can you link the StackOverflow thread to this one (and vice versa); In case other people run into the same issue?
Thanks.
from http-proxy-middleware.
@chimurai I'm having a problems with "res.append", I got error "has no method ".
My project use browser-sync and I check that this dependency has one dependency that use an old version of express 2.5.11 and the method "append" gives the error above.
How can I fix it?
Error description:
res.append(key, proxyRes.headers[key]);
^
TypeError: Object [object Object] has no method 'append'
from http-proxy-middleware.
@dcbasso res.append
is introduced in Express v4.11
More info:
expressjs/expressjs.com#312
from http-proxy-middleware.
@chimurai I know, I'm using this version, but the browsersync have some dependencys that use a old version of the express! Appears to have a conflict!
from http-proxy-middleware.
If you believe it is an issue related to Browser-Sync, please open a ticket at: https://github.com/browsersync/browser-sync/issues
from http-proxy-middleware.
I not really sure... I just can´t understand why this is happens... why my object don't have the information that I need, if i for to use the correct vesion in "package.json".
Could be that I said before? Can be another thing?
from http-proxy-middleware.
It doesn't sound like a http-proxy-middleware bug.
Think StackOverflow would a better place to troubleshoot your personal setup.
from http-proxy-middleware.
Hi, I am facing the same issue but I don't know how to convert this solution to my bs-config.js (lite server)
http://stackoverflow.com/questions/38950292/http-proxy-middleware-how-to-copy-all-cookie-headers
from http-proxy-middleware.
@MadUser. What is the issue you're facing exactly?
from http-proxy-middleware.
Like the original post. I need to pass the session cookie back and forth but my config file looks nothing like the original post. I added link to stack overflow.
from http-proxy-middleware.
This is my configuration:
// Set up the proxy.
if (dev) {
const { createProxyMiddleware } = require('http-proxy-middleware')
server.use(
'/api',
createProxyMiddleware({
target: 'https://api.staging.gocrypto.com/',
changeOrigin: true,
cookieDomainRewrite: 'localhost',
// logLevel: 'debug',
})
)
}
The point is with:
cookieDomainRewrite: 'localhost'
from http-proxy-middleware.
Related Issues (20)
- req.url is always '/' HOT 4
- micromatch vulnerable at v4.0.5 HOT 2
- 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.