Flo
Flo

Reputation: 100

PouchDB sync CORS issue

I try to sync my local pouchdb with a remote one.

I use the last pouchdb and express-pouchdb.

"express-pouchdb": "^1.0.1",
"pouchdb": "^5.2.0"

Server:

var express = require('express'),
    app     = express(),
    PouchDB = require('pouchdb');

var Db = PouchDB.defaults({prefix: '/path/to/db/files/myDb/'});

app.use('/db', require('express-pouchdb')(Db));

var myDb = new Db('myDb')

app.listen(3000);
console.log('Server start on port 3000');

with "add-cors-to-couchdb", I generate the following config

$ add-cors-to-couchdb http://localhost:3000/db
success

./.config.json:

{
  "httpd": {
    "enable_cors": true
  },
  "cors": {
    "credentials": true,
    "methods": "GET, PUT, POST, HEAD, DELETE, OPTIONS",
    "origins": "http://localhost:8080",
    "headers": "accept, authorization, content-type, origin, referer, x-csrf-token"
  }
}

Front:

const db = new PouchDB('localDB', {adapter:'websql'});

db.replicate.to('http://localhost:3000/db/myDb').on('complete', function () {
  console.log("yay, we're done!")
}).on('error', function (err) {
  console.log("boo, something went wrong!", err)
});

Result:

XMLHttpRequest cannot load http://localhost:3000/db/myDb/?_nonce=1452787466740. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

the error message is "Database encountered an unknown error" with statut 500

I've tried to add headers directly:

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://localhost:8080");
    res.header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Credentials", true);
    next();
});

but no more effect ...

Any idea of what I'm doing wrong ?

thanks

Upvotes: 4

Views: 2462

Answers (3)

prabuganesan
prabuganesan

Reputation: 66

Modify your config json like this.

{
  "httpd": {
    "enable_cors": true
  },
  "cors": {
    "credentials": true,
    "methods": "GET, PUT, POST, HEAD, DELETE, OPTIONS",
    "origins": "*",
    "headers": "accept, authorization, content-type, origin, referer, x-csrf-token"
  },
"httpd":{"Bind_address":"0.0.0.0"}
}

Upvotes: 1

Lorezz
Lorezz

Reputation: 101

I've found that https://github.com/pouchdb/pouchdb-server/ works with cors out of the box, so watching the source i've made this in my express app.js

var cors  = require('./libs/cors_pouch'); 
var PouchDB = require('pouchdb');
var pouchDBApp = require('express-pouchdb')(PouchDB);
var config = pouchDBApp.couchConfig;
app.use(cors(config));
app.use('/db', pouchDBApp);

where the cors_pouch is this lib, https://github.com/pouchdb/pouchdb-server/blob/master/lib/cors.js, note that require corser package.

with this settings sync works for me. hope it helps

Upvotes: 1

Corey Webster
Corey Webster

Reputation: 1

I don't know if you figured this out, but I think I got it.

var express = require('express');
var webServer = express();
var dbServer = express();
var PouchDB = require('pouchdb');
var cors = require('cors');

var PouchDBInstance = PouchDB.defaults({prefix: 'db/'});

var db = new PouchDBInstance('foo');

var corsOptions = {
  origin: "*"
};

dbServer.use('/', cors(corsOptions), require('express-pouchdb')(PouchDBInstance));
webServer.use(express.static('public'));

webServer.listen(8080, function() {
  console.log('Web Server listening at http://%s:%s', "0.0.0.0", 8080);
});

dbServer.listen(5984, function() {
  console.log('Web Server listening at http://%s:%s', "0.0.0.0", 5984);
});

You can also set the origin to the real web server location.

On the pouchDB client, you have to set the ajax credentials to false.

var db = pouchDB('http://' + $location.$$host + ':5984/foo',{
  ajax:{
    withCredentials:false
  }
});

Upvotes: 0

Related Questions