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

Feature request: allow server to be sub app in express #1448

Closed
dstudzinski opened this issue Sep 2, 2019 · 7 comments · Fixed by #1455
Closed

Feature request: allow server to be sub app in express #1448

dstudzinski opened this issue Sep 2, 2019 · 7 comments · Fixed by #1455

Comments

@dstudzinski
Copy link
Contributor

dstudzinski commented Sep 2, 2019

Case

Feature request

Issue

Allow server to be part of bigger express application by making changes which allow to use server as a sub app.

Info

  • Environment: Node.js
  • Adapter: websql
  • Stack: ES6

Code

First step is to change code which detects url as an url to collection.
In tunnelCollectionPath https://github.com/pubkey/rxdb/blob/master/src/plugins/server.js#L58 (req.baseUrl === collectionPath) should be changed to something like (req.baseUrl.endsWith(collectionPath))
Another way to achieve this is to add new option to server like prefix which will allow to configure where app is mounted

Second step is to add some parameter to allow constructing app without starting it (calling listen). The app object is all what we need.

Currently I did change described in step one then I did something like this:

...
const {app: dbApp, server} = db.server({
    path: '/', // just main url. It will be mounted on some path later
    port: PORT,  // pass same port like for second app
    cors: false   // we will control CORS headers from our middleware
  });
server.close(); // to shutdown default rxdb server

const app = express();
app.use(// some custom middlewares like cors, other routes);

app.use('/mydbroute', dbApp); // mount rxdb server (dbApp) under /mydbroute

app.listen(PORT) // finally start server

What do you think about such change?
It for sure helps to construct more complicated mocks with eg auth and it's step forward to be able to use rxdb server as an embedded server for small apps.

BTW: I did this example when I got
Access to fetch at 'URI1' from origin 'URI2' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
error and I wanted to add my own middleware to setup CORS.

I did what I described above, disabled cors in server options (important) and added this (the code is not mine I believe I found it here https://stackoverflow.com/a/52210785 or from similar topic):

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
    res.header(
      "Access-Control-Allow-Headers",
      "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept"
    );
    if ("OPTIONS" === req.method) {
      res.sendStatus(200);
    } else {
      next();
    }
  });

which solved my problem

Should I create issue related to CORS?

@pubkey
Copy link
Owner

pubkey commented Sep 2, 2019

For the bring-your-own-express, please see #1156 and #1141 someone started that but did not have the time. This is still a wanted feature so a PR is welcomed.
Yes an issue for the CORS problem would be good. There are currently no browser-tests for the server plugin so I think we should start with that to ensure we can test if the cors are valid.

@dstudzinski
Copy link
Contributor Author

I created issue for CORS with PR.

I will create PR for bring-your-own-express feature but I will be without computer from this Thursday to up to two weeks. I'm not sure if I will be able to prepare PR before Thursday.

dstudzinski pushed a commit to dstudzinski/rxdb that referenced this issue Sep 4, 2019
Closes pubkey#1448 allow server to be sub app in express
@pubkey
Copy link
Owner

pubkey commented Sep 24, 2019

@dstudzinski hi. any update on this?

@dstudzinski
Copy link
Contributor Author

@pubkey Hi, yes, I'm back online and I plan to work on it and finish it tomorrow/during weekend

@dstudzinski
Copy link
Contributor Author

dstudzinski commented Sep 28, 2019

I pushed all changes (I can't find typings for server plugin) but I still have problems with failing tests.
Test which I added fails browser tests. From my understanding /test_tmp/unit.test.js file is used both for Mocha and Karma (all tests are run in node and browser).

New test which I added creates Express app and Express can be started only in node.
How to enable this new serwer test only for node and not run it in Karma?
I believe server plugin should be tested only in Node as it doesn't make sense to run it in browser.

Could you please help/advise me how to solve it? Should I eg reconfigure tests and create new tests file for Node only testing?

@pubkey
Copy link
Owner

pubkey commented Sep 30, 2019

You can prevent test from running in the browser by checking isNode. See here.
Also the tests in server.test.js should only run in node.

@dstudzinski
Copy link
Contributor Author

Ok the problem was in import not in test itself. I moved express import after isNode condition and it works now. Build is green :)

@pubkey pubkey closed this as completed in 8363a67 Oct 4, 2019
pubkey added a commit that referenced this issue Oct 4, 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

Successfully merging a pull request may close this issue.

2 participants