Hi, due to a CORS issue I am trying to use a Netlify function note-fetch as a proxy which is supposed to be an easy fix for CORS. node-fetch/CORS fix I am using “/.netlify/functions/node-fetch” as my API endpoint and then within the node-fetch function I have my actual desired API Endpoint.
The problem I am having is that this /.netlify/functions/node-fetch endpoint is being used within another installed library where a variety of different API calls are being made with different relative paths. These paths are being appended to the url for the API call which is throwing errors. For example, the function is attempting to be called with a URL of: /.netlify/functions/node-fetch/[INSERT APPENDED RELATIVE PATH HERE]
Is there any way I could pull this relative path out of the URL and insert it into the node-fetch function as a parameter? I could then append this relative path to the end of my desired API endpoint before making the call in node-fetch. I have seen something like this with a Javascript Azure Functions using context.bindingData where you can have the function pull out parts of a URL, but I’m just not sure how this would be executed using Netlify functions.
Thanks!
I’m not 100% sure I’ve understood your case, but here are my 2 cents:
The event
object returns quite some useful values like this:
exports.handler = async event => {
console.log(event)
}
What would help you is event.path
which is a path relative to your domain. So, in your code you can do something like:
exports.handler = async event => {
const relPath = event.path.split('/')[4] // should return foo work when URL = /.netlify/functions/node-fetch/foo
}
Do note that, if your relative path has more /
in it, the above method won’t work. If you need that, I suggest using queryParameters
like this: /.netlify/functions/node-fetch?path=foo
. Then in the code:
exports.handler = async event => {
const relPath = decodeURIComponent(event.queryStringParameters.path)
}
The problem with this method is that, your path should be URL encoded and you would have to decode it like the example above.
The error occurs before I could even get into the function to do what you are describing. I’m speculating that it can’t run /.netlify/functions/node-fetch/[INSERT APPENDED RELATIVE PATH HERE] because that file path does not exist. Maybe there needs to be something separating /.netlify/functions/node-fetch and /[INSERT APPENDED RELATIVE PATH HERE] so that the node-fetch function can still execute??
Yes, I had thought that the function won’t run because it won’t be found, but I thought you had tried something and got it working.
Regardless, I think my last solution of using a query parameter would help you achieve what you want.
I think what you described would work if node-fetch function would run regardless of the path that comes after it, but that doesn’t appear to be the case. I also tried to do: /.netlify/functions/node-fetch?relativepath=/[INSERT APPENDED RELATIVE PATH HERE] but that was giving errors too and node-fetch was not able to run.
I think it’s not possible to judge without code and proper use case than the placeholders that you’re currently suggesting.
Generally speaking, what would be the method to pass a property into the node-fetch function via the /.netlify/functions/node-fetch URL? Is that even possible?
You can use Query Parameters. You can also send data as FormData.
I just wanted to close this issue for anyone that may read this thread in the future.
I kept running into a wall with netlify functions and eventually moved over to AWS and created a custom lamba function and proxy there. It may be that there was a way to do this within netlify, but I was never able to find it.
1 Like