我尝试将本地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.错误消息是"Database遇到未知错误“,且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();
});但是没有更多的效果。
你知道我做错了什么吗?
谢谢
发布于 2016-07-19 17:17:03
我发现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包。
有了这个设置,同步对我来说是有效的。希望能有所帮助
发布于 2017-08-31 12:40:44
如下所示修改您的配置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"}
}发布于 2016-02-26 00:38:50
我不知道你是否想通了,但我想我明白了。
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
}
});https://stackoverflow.com/questions/34806910
复制相似问题