Hello,
I am having an issue where my requests are breaking, if I use query parameters, and I really need the query parameters, in order for the site to work properly.
I am doing a site with create react app and redux.
created with this command:
npx create-react-app my-app --template redux
The lambda function is not working when I include query parameters. I can’t tell if it is that the proxy is not working, or if it is that the proxy is working, but the string is just not matching to the exact location of the lambda function. It seems like netlify is trying to match it to a file named “api?search=test”.js … Instead of just “api.js” and then separating the query params at the end.
Can anyone tell me how to fix this?
Thank you!
- My lambda function looks like this:
src/functions/api.js
export async function handler(event, context, callback) { let queryParams = JSON.stringify(event.queryStringParameters); try { callback(null, { statusCode: 200, body: queryParams, }); } catch (error) { callback(error); }
- In my package.json, I have:
"proxy": "http://localhost:9000",
-
If I send this request from the front-end react, IT WORKS (I get a response with an empty object):
axios .get("/.netlify/functions/api") .then((res) => { console.log("from lambda function:", res); }) .catch(console.log);
-
But if I send this request, with query params IT DOES NOT WORK. I get an error that indicates that the request is no longer being proxied to port 9000 (where the functions are being served from).
axios .get("/.netlify/functions/api&search=test") .then((res) => { console.log("from lambda function:", res); }) .catch(console.log);
ERRORS:
xhr.js:178 GET http://localhost:3000/.netlify/functions/api&search=test 500 (Internal Server Error)
Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)Function invocation failed: Error: Cannot find module ‘/Users/me/Desktop/my-react-app/functions/api&playlist=all’
Require stack:
- /usr/local/lib/node_modules/netlify-lambda/lib/serve.js
- /usr/local/lib/node_modules/netlify-lambda/bin/cmd.js
netlify.toml
[build]
command = “CI= react-scripts build”
functions = “functions”
publish = “build”
package.json
local build command:
“dev”: “NODE_ENV=development concurrently "netlify-lambda serve src/functions" "react-scripts start"”