reactjs - ReactiveSearch、ES 和 CORS

标签 reactjs elasticsearch cors reactivesearch

我计划使用 AWS ES 服务来托管我的 Elasticsearch。我按照描述设置了代理 here就像在 Express 服务器中一样。

var proxy = require('express-http-proxy');
...
app.use('/', index);
app.use('/users', users);
app.use('/search', proxy('localhost:9200'));

而且它有效。设置代理非常简单明了。它是 ReactiveSearch 在使用 AWS ES 时推荐的。我可以导航到 localhost:5000/search 并看到 ES 已启动并正在运行(显示默认欢迎消息)。

但是,当我启动我的 React 前端 (create-react-app) 并尝试提交查询时,我收到此错误

Failed to load http://localhost:9200/query-index/_msearch?: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

我相信我的 React 组件中的本地 ES 服务器的 ReactiveSearch 设置正确:

<ReactiveBase
              app="query-index"
              url="http://localhost:9200"
              >

其中query-index是我的ES索引,url是本地ES服务器

我有本地 ES 实例的这些 CORS 设置

http.cors.enabled: true
http.cors.allow-credentials: true
# http.cors.allow-origin: "http://localhost:3000"
# http.cors.allow-origin: /https?:\/\/localhost(:[0-9]+)?/
http.cors.allow-origin: /https?:\/\/(localhost)?(127.0.0.1)?(:[0-9]+)?/
# #http.cors.allow.origin: "*" # this does not work with ReactiveBase
# #http.cors.allow-origin: /https?:\/\/(www.)?elastic.co/
# http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, Content-Type, Content-Length, Authorization, Accept

然后我相信我为我的 Express 服务器设置了正确的 CORS 设置。

var cors = require('cors');
...
var corsOptions = {
  origin: 'http://localhost:9200',
  optionsSuccessStatus: 200
};
...
app.options('/search', cors()); //should enable pre-flight for search routes
app.use('/search', cors(corsOptions), function(req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Length, Content-Type, Authorization, Accept');
  res.header('Access-Control-Allow-Credentials', 'true');
  next();
});

关于 CORS 设置,我做错了什么。是 CORS 路由设置还是 CORS ES 设置?

我计划将 AWS ES 用于生产,但我只想先在本地进行一些测试,但似乎无法为 React 和 ReactiveSearch 设置正确的 CORS。

最佳答案

似乎在 ES cors 配置中,您正在将 https 与本地主机一起使用,这可能会产生错误

http.cors.allow-origin: /https?://(localhost)?(127.0.0.1)?(:[0-9]+)?/

同样在 express 应用程序的 cors 配置中, localhost:3000 是前端吗? 如果是这样,则您将前端列入白名单,但情况可能并非如此。

您可以在声明任何路由之前简单地使用 app.use(cors()) ,然后按照此处所述使用代理搜索路由,稍作更改,

app.use('/search', proxy(options));

https://github.com/appbaseio-apps/reactivesearch-proxy-server/blob/master/index.js#L46

关于reactjs - ReactiveSearch、ES 和 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51833966/

相关文章:

javascript - 如何在 NextJS 中将 next-iron-session 与自定义 api 路由中间件一起使用?

javascript - 如何在组件内为包含图片和 URL 的 LI 项目创建 React Loop?

curl - 从 Graylog 中删除特定日志消息

python - 如何在* aioes *中更改Content-Type

google-chrome - Cross Origin amazon S3 无法使用 chrome

javascript - React 服务器渲染,将 HTML 获取到浏览器

ruby-on-rails - Rails Elasticsearch-多个过滤条件

security - 尝试访问 iframe 内容(不同的子域);尝试设置 CORS

cors - Breeze js : configuring service for CORS

javascript - 错误: 'React' must be in scope when using JSX