我正在尝试将我的 reacetivesearch 应用程序连接到外部弹性搜索提供商(而不是 AWS)。他们不允许对 elasticsearch 集群进行更改,也不允许在集群前使用 nginx。
根据 reacetivesearch 文档,我克隆了代理代码,只对目标和身份验证设置进行了更改(根据下面的代码)。
https://github.com/appbaseio-apps/reactivesearch-proxy-server/blob/master/index.js
代理已成功启动并能够连接远程集群。但是,当我通过代理连接 reacetivesearch 应用程序时,出现以下错误。
在“ http://localhost:7777/testing/_msearch ”访问 XMLHttpRequest ?来自原点“ http://localhost:3000 ”的 CORS 策略已阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头
我使用相同的代理代码对我的本地弹性搜索集群重复了相同的步骤并得到了相同的错误。
只是想知道我们是否需要进行任何额外的更改以确保代理向弹性搜索集群发送正确的请求?我正在使用以下代码作为代理。
const express = require('express');
const proxy = require('http-proxy-middleware');
const btoa = require('btoa');
const app = express();
const bodyParser = require('body-parser')
/* This is where we specify options for the http-proxy-middleware
* We set the target to appbase.io backend here. You can also
* add your own backend url here */
const options = {
target: 'http://my_elasticsearch_cluster_adddress:9200/',
changeOrigin: true,
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader(
'Authorization',
`Basic ${btoa('username:password')}`
);
/* transform the req body back from text */
const { body } = req;
if (body) {
if (typeof body === 'object') {
proxyReq.write(JSON.stringify(body));
} else {
proxyReq.write(body);
}
}
}
}
/* Parse the ndjson as text */
app.use(bodyParser.text({ type: 'application/x-ndjson' }));
/* This is how we can extend this logic to do extra stuff before
* sending requests to our backend for example doing verification
* of access tokens or performing some other task */
app.use((req, res, next) => {
const { body } = req;
console.log('Verifying requests ✔', body);
/* After this we call next to tell express to proceed
* to the next middleware function which happens to be our
* proxy middleware */
next();
})
/* Here we proxy all the requests from reactivesearch to our backend */
app.use('*', proxy(options));
app.listen(7777, () => console.log('Server running at http://localhost:7777 '));
问候