1

I'm attempting to index a user's query using ReactiveSearch's DataSearch component and appbase-js.

So I've made my Node/Express app for appbase-js interaction with appbaseio. in app.js:

...
const search = require('./routes/search');
...
app.use('/api/search', search);

Then here is my search.js

const express = require('express');
const Appbase = require('appbase-js');

// create an appbase.io app's instance
const appbaseRef = new Appbase({
  url: "https://scalr.api.appbase.io",
  app: "index-query",
  credentials: "####-####"
});

const router = express.Router();

/* GET search. */
router.get('/test', (req, res, next) => {
  res.send('This is the SEARCH route - and it WORKS!');
});


router.post('/query', (req, res, next) => {
  appbaseRef.index({
    type: "autocomplete",
    body: value
  }).then('data', response => {
    console.log("@index success: ", response);
  }),('error', error => {
    console.log("@index error: ", error);
  });
});

module.exports = router;

Then here is my DataSearch component:

<DataSearch
   componentId="SearchSensor"
   dataField={["suggestions"]}
   className="search-bar"
   iconPosition="right"
   innerclassName={{
     list: "text-item"
   }}
   onValueSelected{
     (value) => {
      ????
      }
   } 
  />

I was advised in another question not do this:

onValueSelected={(value) => {
    fetch('YOUR_SERVER_URL' or 'Elasticsearch URL', { method: 'POST', body: {...} })
  }

So as not to expose sensitive information on the client

I'm not sure how to get value (the user's query) from my React front end to my Node/Express backend so that it can be indexed to ES app on Appbaseio?

Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47
user3125823
  • 1,846
  • 2
  • 18
  • 46

1 Answers1

0

Say your server is hosted at 'SERVER_URL', the key is to send the data from the frontend to the server via a fetch request:

<DataSearch
  ...
  onValueSelected={(value) => {
    fetch('SERVER_URL/api/search/query', {
      method: 'POST',
      body: JSON.stringify({ value })
    }).then(() => handle response client side))
  }}
/>

Then you can add the body-parser middleware in express.

app.use(bodyParser.json())

In your route you can use the value from body and index it to elasticsearch. You can use the index method from appbase-js which you're using here.

router.post('/query', (req, res, next) => {
  appbaseRef.index({
    type: "autocomplete",
    body: { value: req.body.value }
  }).then('data', response => {
    console.log("@index success: ", response);
  }),('error', error => {
    console.log("@index error: ", error);
  });
});
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47
  • thanks for the great answer! To my understanding basically what's going on is I need a small Node/Express app that has appbase-js installed and setup to interact with my Appbaseio ES app... is that right? After I get his all done, I'm just about ready to go. – user3125823 Aug 23 '18 at 13:29
  • the SERVER_URL should be localhost:5000/api/search/query (for now) and NOT the Appbase url, right? – user3125823 Aug 23 '18 at 13:43
  • Yeah it should be your backend url – Divyanshu Maithani Aug 23 '18 at 13:48
  • I'm getting a "Access Control Allow Origin" error with a 500 error code. Do I need to setup a proxy on the localhost server? So basically the small Node/Express app handling the query index request and response should act as a proxy server for my Appbaseio ES app? – user3125823 Aug 23 '18 at 13:53
  • This could be a cors issue. Could you try adding [`cors`](https://www.npmjs.com/package/cors) middleware – Divyanshu Maithani Aug 23 '18 at 16:41
  • I got it working by following the example on Github for a proxy server using http-proxy-middleware. When I go to localhost:5000/api/search/query - Chrome asks me for UN and PW - I've never used this package, not sure if that is normal. Is CORS better for production? – user3125823 Aug 23 '18 at 21:15
  • I thought you were trying to send request cross domain (where cors comes in). – Divyanshu Maithani Aug 24 '18 at 06:25
  • Looking over the docs for ReactiveSearch, I saw the proxy example for client and server and so just followed the server example to get things working. However now that you say it - I guess I am trying to send requests in a cross domain fashion and CORS would need to be set up - Many Thanks for all your help! – user3125823 Aug 24 '18 at 12:39