[elastic/app-search-node] Cannot destroy documents due to CORS issue

Hi, I'm using the app-search node js client library in my web application. I'm able to index, and search for documents but when I try to destroy them I receive the following error:

Access to fetch at '{redacted}/documents' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Any idea how to fix this?

Hey @terell, the correct way to hit the index or delete endpoints from a browser is to set up your own server side endpoints which then call index or delete. This will solve your CORS issue, and also alleviate 2 other issues:

  1. You should probably not be using the App Search node client in browser based JavaScript. We have a JavaScript client which is intended for browser usage: https://github.com/elastic/app-search-javascript.

  2. The index and delete endpoints require a Private Key, which you would not want to expose in browser JavaScript. The only key which is considered safe to expose is the Search Key.

Hope this helps,

Jason

Thanks @JasonStoltz! That worked.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.