Code Monkey home page Code Monkey logo

Comments (14)

olalonde avatar olalonde commented on May 22, 2024 2

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.

chimurai avatar chimurai commented on May 22, 2024 1

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.

 avatar commented on May 22, 2024

@chimurai : thanks for the response, it works. will use stack overflow for further information.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 22, 2024

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.

dcbasso avatar dcbasso commented on May 22, 2024

@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.

chimurai avatar chimurai commented on May 22, 2024

@dcbasso res.append is introduced in Express v4.11

More info:
expressjs/expressjs.com#312

from http-proxy-middleware.

dcbasso avatar dcbasso commented on May 22, 2024

@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.

chimurai avatar chimurai commented on May 22, 2024

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.

dcbasso avatar dcbasso commented on May 22, 2024

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.

chimurai avatar chimurai commented on May 22, 2024

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.

MadUser avatar MadUser commented on May 22, 2024

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.

chimurai avatar chimurai commented on May 22, 2024

@MadUser. What is the issue you're facing exactly?

from http-proxy-middleware.

MadUser avatar MadUser commented on May 22, 2024

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.

alenvlahovljak avatar alenvlahovljak commented on May 22, 2024

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)

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.