首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ReactiveSearch的弹性搜索CORS错误

ReactiveSearch的弹性搜索CORS错误
EN

Stack Overflow用户
提问于 2018-06-06 23:50:56
回答 2查看 760关注 0票数 2

我已经有一个用于jwt身份验证的Node/Express服务器设置,可以与我的Create React应用程序一起使用。我正在使用CORS npm包和简单的中间件app.use(cors());来解决预期的CORS相关问题,并且它工作得很好。

现在,我想将Elasticsearch和ReactiveSearch添加到我的React应用程序中,并试图找出如何使用本地ES实例来克服CORS问题。

我的第一个猜测是,我需要创建一个单独的searchRoutes.js文件,并在那里实现ES本地工作和连接我的React应用程序所需的任何CORS代码?

我一直在遵循ReactiveSearch的文档(使用http-proxy-中间件),但没有取得任何成功……继续获取此CORS错误:无法加载http://localhost:9200/query-index/_msearch?:Request header字段content-type不允许Access-Control-Allow-Headers在印前检查响应中使用。

尽管我相信我已经实现了CORS预检solution

代码语言:javascript
复制
app.options('/autocomplete', cors()); // this is for pre-flight

欢迎任何建议、链接或教程

更新:这是我的Nodejs服务器( index.js )

代码语言:javascript
复制
require('dotenv').config();
import express from 'express';
import http from 'http';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import mongoose from 'mongoose';
import proxy from 'http-proxy-middleware';
import cors from 'cors';
import compression from 'compression';

// import dotenv from 'dotenv'; // added
import router from './router';
// import { dbConfig } from './config';

// Express app init
const app = express();
// app.options('/query-index', cors()); // this is for pre-flight

/* 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: 'https://scalr.api.appbase.io/',
    target: 'http://localhost:9200',
    changeOrigin: true,
    // onProxyReq: (proxyReq, req) => {
    //     proxyReq.setHeader(
    //         'Authorization',
    //         `Basic ${btoa('cf7QByt5e:d2d60548-82a9-43cc-8b40-93cbbe75c34c')}`
    //     );
    //     /* 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);
    //         }
    //     }
    // }
};

// Connect MongoDB
mongoose.connect(process.env.MONGODB_URI);
mongoose.set('debug', true);

// middleware
app.use(compression());
app.use(morgan('combined'));
app.use(cors()); // cors middleware
// https://stackoverflow.com/a/38345853/3125823
// Enable CORS from client-side from slatepeak
// app.use((req, res, next) => {
//   res.header('Access-Control-Allow-Origin', 'http://localhost:3333', 'http://localhost:9200'); // this can also be a list of urls
//   res.header('Access-Control-Allow-Methods', 'OPTIONS, PUT, GET, POST, DELETE');
//   res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-Auth-Token, Origin, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials');
//   // res.header('Access-Control-Allow-Credentials', 'true');
//   next();
// });
app.use(bodyParser.json({ type: '*/*' }));

/* 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('/query-index', (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('/query-index', proxy(options));

app.options('/query-index', cors()); // this is for pre-flight
app.get('/query-index', cors(), function(req, res, next) {
  res.json({ msg: 'This is CORS-enabled for route \/query-index'});
});

router(app);

const authPort = process.env.PORT || 3333; // default
const authServer = http.createServer(app);
authServer.listen(authPort);
console.log('Auth Server listening on:', authPort);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-07 00:38:03

首先,你可以去掉它,简单明了地为所有请求启用CORS:

代码语言:javascript
复制
app.use(cors())

让我们假设你仍然想要单路由:你的代码引用了一个路由"autocomplete",你发布的返回CORS错误的链接引用了一个不同的路由"query_index/..",因为你选择了单路由启用方式,你需要修复这个问题,并使用你启用CORS的匹配路由。

从你发布的内容来看,你的解决方案看起来还不完整。如果你看看solution

代码语言:javascript
复制
app.get('/products/:id', cors(), function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for a Single Route'})
})

并将其与您的进行比较,您应该向您的路由添加一个回调,因为回调是您的逻辑发生的地方(传输到控制器等)。这可能看起来像这样:

代码语言:javascript
复制
app.get('/autocomplete', cors(), function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for route \/autocomplete'})
})
票数 1
EN

Stack Overflow用户

发布于 2021-05-09 22:31:09

代码语言:javascript
复制
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-origin: '*'
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept 

您需要将这些cors权限添加到elasticsearch.yml配置文件中

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50724605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档