Created
February 15, 2017 14:29
-
-
Save davengeo/72d93667936da1b38cb6e2c975d23328 to your computer and use it in GitHub Desktop.
fix for protocol mismatch between sync-gw and pouchdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'), | |
request = require('request'), | |
_ = require('lodash'), | |
config = { syncGwAdmin: 'http://localhost:4985', db: 'my-db' }; | |
let router = express.Router(); | |
let rebuildDocs = function(body) { | |
const parts = body.split(/\r\n|\r|\n/g); | |
return _.reduce( | |
_.filter(parts, part => _.startsWith(part, '{')).map(JSON.parse), | |
(result, item) => { | |
result.push({ ok: item }); | |
return result; | |
}, | |
[]); | |
} | |
router.post('', (req, res) => { | |
const url = `${config.syncGwAdmin}/${config.db}/_bulk_get` | |
console.log(`calling to \'multipart/mixed\' _bulk_get POST ${url}`) | |
return request.post({ | |
url: url, | |
json: true, | |
headers: { accept: 'multipart/mixed' }, | |
qs: req.query, | |
body: req.body | |
}, (error, response, body) => { | |
console.log('rebuilding response to \'application/json\''); | |
res.status(200).json({ results: [{ docs: rebuildDocs(body) }] }).end(); | |
}); | |
}) | |
; | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment