【发布时间】: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();
});
但没有更多效果...
知道我做错了什么吗?
谢谢
【问题讨论】:
-
你有过这个错误吗?