【问题标题】:PouchDB sync CORS issuePouchDB 同步 CORS 问题
【发布时间】:2016-04-20 19:14:09
【问题描述】:

我尝试将本地 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.

错误消息是“数据库遇到未知错误”,状态为 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();
});

但没有更多效果...

知道我做错了什么吗?

谢谢

【问题讨论】:

  • 你有过这个错误吗?

标签: express pouchdb


【解决方案1】:

像这样修改你的配置 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"}
}

【讨论】:

    【解决方案2】:

    我发现 https://github.com/pouchdb/pouchdb-server/ 可以开箱即用地与 cors 一起使用,因此请查看我在我的 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);
    

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

    使用此设置同步对我有用。希望对你有帮助

    【讨论】:

      【解决方案3】:

      我不知道你是否明白这一点,但我想我明白了。

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-23
        • 2018-10-28
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        相关资源
        最近更新 更多