简体   繁体   中英

How to make pouchdb cloudant replication work with node.js/express proxy?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM