我正在尝试设置PouchDB以在浏览器(React + Redux堆栈)和我的数据库之间同步节点代理(使用JWT检查身份验证)。这是我的代理代码:
this.app.all('/database/*', bodyParser.urlencoded({extended: true}), bodyParser.json(), (req, res) => {
// req.pause();
const headers = this.auth.proxyRequestDecorator()({headers: {...req.headers}}, req);
const remoteUrl = req.url.slice(10);
const opts = Object.assign({
method: req.method,
url: `http://${config.get('couchHost')}:${config.get('couchPort')}/${remoteUrl}`,
...headers,
}, req.method !== 'GET' ? {body: JSON.stringify(req.body)} : {});
mainStory.info('http', `${req.method} ${req.url} -> http://${config.get('couchHost')}:${config.get('couchPort')}/${remoteUrl}`, {
attach: opts,
attachLevel: 'trace'
});
const remoteReq = request(opts).pipe(res);
});
以下是我用于在浏览器中同步数据库的代码:
function syncDB (dbs: string[], max: number): thunk.ThunkAction<void, Defs.CompactdState, void> {
return (dispatch, getState) => {
const dbName = dbs[0];
const db = new PouchDB(dbName);
const remote = getDatabase(dbName);
db.sync(remote).on('complete', (info) => {
dispatch({
type: UPDATE_SYNC,
progress: (max - dbs.length + 1) / max
});
db.sync(remote, {live: true}).on('change', (info) => {
console.log(info);
}).on('error', (err) => {
console.log(dbName, err);
}).on('paused', function (info) {
console.log(dbName+' pause', info);
});
if (dbs.length > 1) {
return (syncDB(dbs.slice(1), max) as any)(dispatch, getState);
} else {
setTimeout(() =>
dispatch({
type: END_SYNC
}), 250);
}
});
}
}
function sync (): thunk.ThunkAction<void, Defs.CompactdState, void> {
return (dispatch, getState) => {
if (getState().app.syncing) {
return;
}
const dbs = [ 'artists', 'albums', 'tracks', 'files', 'trackers'];
(syncDB(dbs, dbs.length) as any)(dispatch, getState);
}
}
但是当我在浏览器中创建一个新的tracker
文档时,它不会被同步,如果我清除网站数据,并重新加载页面,则新文档不存在(也不在couchdb ui中)
这是日志:
trackers pause undefined
trackers pause undefined
artists CustomPouchError {code: "ETIMEDOUT", status: 0, result: {…}}
albums CustomPouchError {code: "ETIMEDOUT", status: 0, result: {…}}
tracks CustomPouchError {code: "ETIMEDOUT", status: 0, result: {…}}
files CustomPouchError {code: "ETIMEDOUT", status: 0, result: {…}}code: "ETIMEDOUT"result: {ok: false, start_time: Sun Sep 17 2017 19:58:29 GMT+0200 (Paris, Madrid (heure d’été)), docs_read: 0, docs_written: 0, doc_write_failures: 0, …}status: 0__proto__: Error
trackers CustomPouchError {code: "ETIMEDOUT", status: 0, result: {…}}
当f5被压缩到连接超时时,它还会阻止页面重新加载