简体   繁体   English

响应式搜索、ES 和 CORS

[英]ReactiveSearch, ES and CORS

I plan on using AWS ES service to host my Elasticsearch.我计划使用 AWS ES 服务来托管我的 Elasticsearch。 I setup a proxy as described here like so in Express server.我设置为描述代理这里的Express服务器等等等等。

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

And it works.它有效。 Setting up the proxy was pretty simple and straightforward.设置代理非常简单明了。 And its what ReactiveSearch recommends when using AWS ES.以及它在使用 AWS ES 时 ReactiveSearch 推荐的内容。 I can navigate to localhost:5000/search and see that ES is up and running (displays default welcome message).我可以导航到 localhost:5000/search 并看到 ES 已启动并正在运行(显示默认欢迎消息)。

However, when I start my React front-end(create-react-app) and try to submit a query I get this error但是,当我启动 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.

I believe I have the settings right for ReactiveSearch for a local ES server in my React component:我相信我在我的 React 组件中为本地 ES 服务器设置了正确的 ReactiveSearch 设置:

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

Where query-index is my ES index and url is local ES server其中 query-index 是我的 ES 索引,url 是本地 ES 服务器

I have these CORS settings for local ES instance我有本地 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

And then I believe I have the correct CORS setting setup for my Express server.然后我相信我为我的 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();
});

What am I doing wrong when it comes to the CORS settings.当涉及到 CORS 设置时,我做错了什么。 Is it the CORS route setup or CORS ES settings?是 CORS 路由设置还是 CORS ES 设置?

I plan to use AWS ES for production but I just want to do some testing locally first and can't seem to get the CORS setting right for React and ReactiveSearch.我计划将 AWS ES 用于生产,但我只想先在本地进行一些测试,似乎无法为 React 和 ReactiveSearch 设置正确的 CORS 设置。

Seems like in ES cors config you are using https with localhost, which might be creating error似乎在 ES cors 配置中,您将https与 localhost 一起使用,这可能会产生错误

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

Also in your cors configuration for express app, Is localhost:3000 for frontend?同样在 Express 应用程序的 cors 配置中,前端是否为localhost:3000 If so, you are whitelisting frontend which might not be the case.如果是这样,您将前端列入白名单,但情况可能并非如此。

You can simply use app.use(cors()) before any route is declared and then use proxy for your search route as mentioned here, with a minor change,您可以在声明任何路线之前简单地使用app.use(cors()) ,然后按照此处所述为您的搜索路线使用代理,稍作更改,

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM