Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxDB server path option problem #1447

Closed
dstudzinski opened this issue Sep 2, 2019 · 1 comment
Closed

RxDB server path option problem #1447

dstudzinski opened this issue Sep 2, 2019 · 1 comment

Comments

@dstudzinski
Copy link
Contributor

dstudzinski commented Sep 2, 2019

Case

Bug

Issue

path option for server is broken. You can't start server with path other than default

Info

  • Environment: Node.js, rxdb v8.4.0
  • Adapter: websql
  • Stack: ES6

Code

When you start server like this

return db.server({
    path: '/db2', // (optional)
    port,  // (optional)
    cors: true   // (optional), enable CORS-headers
  });

You can access express-pouchdb url /db2 but you can't access collections urls like /db2/mycollectionname because of error

{
  "error": "not_found",
  "reason": "no_db_file"
}

I think it's because of this line https://github.com/pubkey/rxdb/blob/master/src/plugins/server.js#L58

where /db/ string is hardcoded instead of the use of path value const toFull = req.originalUrl.replace('/db/' + colName, '/db/' + to);

Second thing is that in tunnelCollectionPath function / is added to path without checking path value so for example you can't use just / as a path.

I was able to fix it quickly with this changed code:

function tunnelCollectionPath(db, path, app, colName) {
  db[colName].watchForChanges();

  const pathWithSlash = path.endsWith('/') ? path : path + '/';

  app.use(pathWithSlash + colName, function (req, res, next) {
    if (req.baseUrl === pathWithSlash + colName) {
      var to = normalizeDbName(db) + '-rxdb-0-' + colName;
      var toFull = req.originalUrl.replace(pathWithSlash + colName, pathWithSlash + to);
      req.originalUrl = toFull;
    }

    next();
  });
}

Should I create a PR for this?

@pubkey
Copy link
Owner

pubkey commented Sep 2, 2019

@dstudzinski thx for reporting. I fixed this and added some tests.

@pubkey pubkey closed this as completed Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants