Http://localhost:3000/.netlify/functions/search 500 (Internal Server Error)

I try to run the repository at https://github.com/elastic/search-ui/tree/master/examples/elasticsearch, which includes a Netlify function that proxies my Elasticsearch instance.

The function looks like:
/*
2 This is a Netlify Function that proxies our Elasticsearch instance.
3 */
4 import fetch from “node-fetch”;
5
6 exports.handler = function(event, context, callback) {
7 const host = process.env.ELASTICSEARCH_HOST;
8
9 fetch(${host}/national-parks/_search, {
10 method: “POST”,
11 headers: { “content-type”: “application/json” },
12 body: event.body
13 })
14 .then(response => response.text().then(body => [response, body]))
15 .then(([response, body]) => {
16 callback(null, {
17 statusCode: response.status,
18 body: body
19 });
20 })
21 .catch(() => {
22 callback(null, {
23 statusCode: 500,
24 body: “An error occurred”
25 });
26 });
27 };
~

A netlify.toml file is also included,
1 [build]
2 functions = “netlify-lambda”
3 publish = “build/”
4 command = “npm run build”

I got error when I visit localhost:3000, which says:
http://localhost:3000/.netlify/functions/search 500 (Internal Server Error)

I did find some similar posts but not like my settings.
Can any one give a hint how to solve it?

Thank you.

Hey @lxf,

Some reading material for you as to why this might happen.

Firstly, are any applicable packages out-of-date? See this or this, for example. Unlikely given your setup but worth checking and considering!

Secondly, can you check whether your JSON syntax is valid? See this. Quite likely!

Lastly, is everything where it should be? See this. Depending on your build directory, this could be throwing a spanner in the works.

Your function is likely having issues connecting to your local Elasticsearch.

I put a fix in for search-ui so that the error is logged more prominently, which should help you debug.

Jason

1 Like