pouchdb / pouchdb-server

CouchDB-compatible server built on PouchDB and Node
Apache License 2.0
951 stars 154 forks source link

Enable Cors on Pouchdb and Fauxton #360

Open bishal1994 opened 5 years ago

bishal1994 commented 5 years ago

Hello , I am creating a Cordova-Android app that runs express-pouch server in its backend. After going through all the issues and discussions, I tried to implement the

        overrideMode: {
         include: ['routes/fauxton']
         }

I got the fauxton but the configuration tab is blank , for which I am unable to enable the CORS. Then I tried to provide the config.json file enabling the CORS but still CORS was not activated. I tried to download the pouchdb-fauxton and did the npm link and also linked to the express-pouch but no luck.

config.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"}
 }

Tried to use CORS package and tried to activate in express

main.js

var argv = require('minimist')(process.argv.slice(2));
var logLocation=argv.pouchlog; 
var express = require('express');
var app = express(); 
var PouchDB = require('pouchdb');
var cors = require('cors');

   let InMemPouchDB = PouchDB.defaults({db: require("memdown"), migrate: true})
   let pouchHandle = require('express-pouchdb')(InMemPouchDB, 
   {mode:'minimumForPouchDB',configPath:'./config.json',logPath:logLocation });
   app.use("/",cors(corsConfig),pouchdbHandle);
   app.listen(3000);
   let db = new InMemPouchDB('test');
   db.changes({live:true}).on('change',console.log);

Please can anyone help me with activating the CORS on the express-pouch server ? Any help will be greatly appreciated.

Bishalsahoo commented 5 years ago

Even though I enable the cors via couchConfig.set but still it doesn't sync with the couches on another domain. It only works for local network syncing . Please guys need some help in this context ,I am new to all these.

BillMeyerRSA commented 4 years ago

Even though I enable the cors via couchConfig.set but still it doesn't sync with the couches on another domain. It only works for local network syncing . Please guys need some help in this context ,I am new to all these.

Were you able to figure this out? Having issues with getting the config.json issues figured out

Bishalsahoo commented 4 years ago

@BillMeyerRSA if you are talking about cors then I wasn't able to make it work with the cors library that I used above. So I had to make my own and it worked fine. But as of config.json I guess not switched to other libs due to lack of support here

richardeschloss commented 1 year ago

To get CORS working with the express app, I had to use pouchdb-server's cors library:

const PouchDB = require('pouchdb');
const express = require('express');
const expressPouch = require('express-pouchdb')
const cors = require('pouchdb-server/lib/cors')

const app = express();

const pouchDBApp = expressPouch(PouchDB, {
    mode: 'fullCouchDB',
    configPath: './config.json'  <-- defaults to this
})

app.use(cors(pouchDBApp.couchConfig)) // <-- needed
app.use(pouchDBApp);
app.listen(8000, () => {
    console.log('listening on ==> http://localhost:8000')
});

With the following set in config.json:

{
  "cors": {
    "methods": "GET, PUT, POST, HEAD, DELETE",
    "origins": "*",
    "headers": "accept, authorization, content-type, origin, referer"
  },
  "httpd": {
    "enable_cors": true
  }
}