1

I'm unable to get PouchDB Cloudant replication to work over a proxy (express/node.js server and node-http-proxy). Would like to achieve this to add access control.

Replication works without proxy:

PouchDB --> Cloudant (https://account:password@account.cloudant.com/testdb)

Replication fails with proxy:

PouchDB --> express proxy (http://localhost:3000/proxy) --> Cloudant (https://account:password@account.cloudant.com/testdb)

error: CustomPouchError

Proxy

const httpProxy = require("http-proxy");
const proxy = httpProxy.createProxyServer();

router.all("/proxy", (req, res, next) => {
  proxy.on("proxyReq", (proxyReq, req, res, options) => {
    proxyReq.setHeader("Authorization", "Basic: Base64(account:password)")
  })

  proxy.web(req, res, {
    target: "https://account:password@account.cloudant.com/testdb",
    secure: false,
    changeOrigin: true
  });
});

PouchDB

// succeeds without proxy
localDB.replicate
  .to("https://account:password@account.cloudant.com/testdb")
  .on('error', err => {
    console.log('error', err);
  });

// fails with proxy
localDB.replicate
  .to("http://localhost:3000/proxy")
  .on('error', err => {
    console.log('error', err);
  });

Really stuck on this! Really appreciate any thoughts on what's wrong or how to achieve pouchdb cloudant replication over a proxy. Thank you!

Pierre Teo
  • 11
  • 1
  • 4

1 Answers1

0

Your code is attempting to replicate with the proxy itself. The proxy is not the destination server, which is why you get an error. If your proxy settings in Express are correct you should be able to replicate with the target server and the proxy settings should control the connection without any extra code in your application.

IanC
  • 865
  • 8
  • 11