PouchDB同步CORS问题

时间:2016-01-15 08:26:02

标签: express cors pouchdb

我尝试将本地pouchdb与远程pouchdb同步。

我使用最后一个pouchdb和express-pouchdb。

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

服务器:

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');

使用" add-cors-to-couchdb",我生成以下配置

$ 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"
  }
}

前:

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)
});

结果:

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.

错误消息是"数据库遇到未知错误"与statut 500

我试图直接添加标题:

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();
});

但没有更多效果...

知道我做错了什么?

感谢

3 个答案:

答案 0 :(得分:0)

我不知道你是不是想出来了,但我想我明白了。

Vector

您还可以将原点设置为真实的Web服务器位置。

在pouchDB客户端上,您必须将ajax凭据设置为false。

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);
});

答案 1 :(得分:0)

我发现https://github.com/pouchdb/pouchdb-server/可以与开箱即用的cors一起工作,所以看着我在我的快递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);

其中cors_pouch是这个lib,https://github.com/pouchdb/pouchdb-server/blob/master/lib/cors.js,请注意需要corser包。

使用此设置同步适用于我。希望它有所帮助

答案 2 :(得分:0)

像这样修改你的配置json。

{
  "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"}
}