У меня уже есть настройка сервера Node / Express для аутентификации jwt, работающая с моим приложением Create React.Я использую пакет CORS npm и простое промежуточное ПО app.use(cors());
для решения ожидаемых проблем, связанных с CORS, и его исправная работа.
Теперь я хочу добавить Elasticsearch и ReactiveSearch в мое приложение Reactи я пытаюсь выяснить, как преодолеть проблемы CORS с моим локальным экземпляром ES.
Мое первое предположение заключается в том, что мне нужно создать отдельный файл searchRoutes.js и реализовать любой код CORS, необходимый для локальной работы ES.и соединиться с моим приложением React?
Я следил за документами для ReactiveSearch (используя http-proxy-middleware) и не добился успеха ... продолжаю получать эту ошибку CORS: Не удалось загрузить http://localhost:9200/query-index/_msearch?: Тип содержимого поля заголовка запроса не разрешен Access-Control-Allow-Headers в ответе предполётной проверки.
Несмотря на то, что я считаю, что я реализовал решение предварительной проверки CORS
app.options('/autocomplete', cors()); // this is for pre-flight
Любые предложения, ссылки или учебники приветствуются
ОБНОВЛЕНИЕ: это мой Nodejs index.js (сервер)
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);