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

RxServer cors settings seem to only apply to OPTIONS when using ExpressAdapter #6523

Closed
snimmagadda1 opened this issue Nov 2, 2024 · 4 comments

Comments

@snimmagadda1
Copy link

snimmagadda1 commented Nov 2, 2024

I've been fiddling with RxServer in node, trying to replicate data from an angular app, and chose the rxdb-server package + ExpressAdapter for a QuickStart. I've verified the rest/replication endpoints working via CURL 👍 ; however, I'm noticing that when trying to make requests from my client (localhost:4200) the CORS settings seem to only be set on the OPTIONS responses:

CORS_express_network

Looking at the RxServerAdapterExpress class, it seems setCors is indeed only setting OPTIONS. Is this intended? The docs here are a bit ambiguous on this point:

export const RxServerAdapterExpress: RxServerAdapter<Express, Request, Response> = {
    async create() {
        const app = express();
        app.use(express.json());
        return app;
    },
    setCors(serverApp, path, cors) {
        serverApp.options('/' + path + '/*', expressCors({
            origin: cors,
            // some legacy browsers (IE11, various SmartTVs) choke on 204
            optionsSuccessStatus: 200
        }));
    },

My server setup:

DB_INSTANCE = await _createDb();

const rxServer = await createRxServer({
  database: DB_INSTANCE as unknown as RxDatabase,
  port: 3000,
  adapter: RxServerAdapterExpress,
  cors: "http://localhost:4200",
});

// events endpoint 
const endpoint = await rxServer.addRestEndpoint({
  name: "events",
  collection: DB_INSTANCE.events,
  cors: "http://localhost:4200",
});

console.log("RxServer: endpoint created ", endpoint.urlPath);

// replication endpoint 
const replicationEndpoint = await rxServer.addReplicationEndpoint({
  name: "events-rpl",
  collection: DB_INSTANCE.events,
  cors: "http://localhost:4200",
});
console.log("RxServer: rpl endpoint created ", replicationEndpoint.urlPath);

await rxServer.start();
@snimmagadda1 snimmagadda1 changed the title RxServer cors settings seem to only apply to OPTIONS requests when using ExpressAdapter RxServer cors settings seem to only apply to OPTIONS when using ExpressAdapter Nov 2, 2024
@snimmagadda1
Copy link
Author

fwiw for my testing, I fixed this by forking the server and changing to serverApp.use('/' + path + '/*', expressCors({ as opposed to serverApp.options('/' + path + '/*', expressCors({ . Not sure if this is a change wanted in main

@pubkey
Copy link
Owner

pubkey commented Nov 7, 2024

Hi @snimmagadda1 you fix looks good. Please make a PR with your fix. Also make sure to include a test case.

@snimmagadda1
Copy link
Author

sure here's a quick one.. pubkey/rxdb-server#160

@pubkey
Copy link
Owner

pubkey commented Nov 10, 2024

Thank you, merged.

@pubkey pubkey closed this as completed Nov 10, 2024
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