pouchdb / pouchdb-server

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

fauxton does not work when attached as express-middeware (app.use('/db',require('express-pouchdb')(...))) due to build on "initialize.js.underscore",app.host variable,etc #180

Open nolanlawson opened 7 years ago

nolanlawson commented 7 years ago

From @thesli on October 28, 2014 15:46

The host is no longer correct when you attach as middleware. In the case app.use('/db',...), a.host's value should be '/db/'

In require.js(build from apache/couchdb-fauxton:app/initialize.js.underscore)

define("initialize", [], function() {
        var a = {
            root: "/_utils/fauxton/",
            version: "1.0",
            host: "../.."
        };
        return a
    })

The core/couchdbSession have the same issue of using "/_session" without refering correct location.

define("core/couchdbSession", ["core/base"], function(a) {
        var b = {
            Session: a.Model.extend({
                url: "/_session",
                user: function() {...

Also the clipboard part

f.Clipboard = b.View.extend({
            initialize: function(c) {
                this.$el = c.$el, this.moviePath = b.getExtensions("zeroclipboard:movielist")[0], _.isUndefined(this.moviePath) && (this.moviePath = a.host + a.root + "js/zeroclipboard/ZeroClipboard.swf") ...

The setting should be:

url: "/_session" -> url: "./_session"
host: "../.." -> host: "./"
this.moviePath = a.host + a.root + "js/zeroclipboard/ZeroClipboard.swf" 
-> 
this.moviePath = a.host + "js/zeroclipboard/ZeroClipboard.swf"

Please kindly rebuild the asset and serve the correct path.

Copied from original issue: pouchdb/express-pouchdb#124

nolanlawson commented 7 years ago

From @thesli on October 28, 2014 16:2

And also to make the security tag work,it needs to change the permission url handling to realtive path

define("addons/permissions/resources", ["app", "api"], function(a, b) {
        var c = b.addon();
        return c.Security = Backbone.Model.extend({
            defaults: {
                admins: {
                    names: [],
                    roles: []
                },
                members: {
                    names: [],
                    roles: []
                }
            },
            isNew: function() {
                return !1
            },
            initialize: function(a, b) {
                this.database = b.database
            },
            url: function() {
                return "./" + this.database.safeID() + "/_security"
            },
            addItem: function(a, b, c) {
                var d = this.get(c);
                return d && d[b] ? d[b].indexOf(a) > -1 ? {
                    error: !0,
                    msg: "Role/Name has already been added"
                } : (d[b].push(a), this.set(c, d)) : {
                    error: !0,
                    msg: "Section " + c + "does not exist"
                }
            }
        })
return window.location.origin + "/" + this.database.safeID() + "/_security" 
-> 
return "./" + this.database.safeID() + "/_security"
nolanlawson commented 7 years ago

From @MichaelJCole on October 28, 2014 16:35

@thesli looks great! Thanks!

If no-one else wants to package this up, I'll make a new PR with this fix and the README changes around it.

nolanlawson commented 7 years ago

From @nickcolley on October 28, 2014 21:58

Can we push this upstream?

nolanlawson commented 7 years ago

From @MichaelJCole on November 8, 2014 21:35

Hi, I made these changes, did some testing, made some more changes, and made a PR in the Fauxton repo. https://github.com/pouchdb/couchdb-fauxton/pull/5

If accepted, there, I'll make a PR for here.

Cheers!

Mike

nolanlawson commented 7 years ago

Looks like this was fixed in https://github.com/pouchdb/pouchdb-fauxton/pull/5 but it needs to be rebased on github.com/apache/couchdb-fauxton

MichaelJCole commented 7 years ago

Hi @nolanlawson, wanted to use PouchDB for another project and found this issue and my hacky old commit. lol.

Hey, could the base tag help here? I just don't know enough about PouchDB/CouchDB/Fauxton to make a real solution.

garrensmith commented 7 years ago

@MichaelJCole I think this issue has to be solved at the Fauxton level. It is possible. But we have never done any work in Fauxton to support it.

KpjComp commented 7 years ago

@MichaelJCole Not sure the base tag will help, as it says specifies the base URL to use for all relative URLs, the problem is Fauxton is specifying absolute url's :(

I've posted an issue to "couchdb-fauxton", it's been tagged "enhancement","help needed".
https://github.com/apache/couchdb-fauxton/issues/944

Not been able to use Fauxton inside a none root url, is a bit of a show stopper for me. It just doesn't feel right mixing database 'urls' with application / web urls.. Think of the confusion's if we created a database called img / css / assets etc.. :(

I'm pretty new to PouchDb/CouchDB, so I might not be the best person to help, but if there is nobody else who can, I'll give it a stab. My biggest problem is I'm not a massive Git user, last time I helped was with phantomjs-node, I remember been out of my comfort zone, with all this git push, pull, etc.. :)

natew commented 6 years ago

Downstream temporary patch would be much appreciated :)

MrOggy85 commented 6 years ago

This workaround fixed it for me (Express 4):

app.use('/example', ExampleRouter);

// Forward all unknown calls to PouchDb
app.use(function(req,res,next) {
  return PouchDbRouter.handle(req, res, next);
});

One big caveat here is concerning naming. The name of the database cannot be the same as your other routes. If you look in the request log, Fauxton is making a lot of different requests so be careful! Possible solution is just to prefix your databases to avoid collision.

garrensmith commented 6 years ago

This commit in Fauxton https://github.com/apache/couchdb-fauxton/commit/8a3e520e17e2e79ee6015861f2592fadcb944558 should fix this. It would just require building the latest version of Fauxton for express-pouch