I’m trying to get redirects to work with a local function. I have a simple redirect rule in my netlify.toml
file:
[[redirects]]
from = "/api/v1/:splat"
to = "/.netlify/functions/main/"
status = 200
force = true
And a main function that was generated by the netlify functions:create
command:
const handler = async (event) => {
try {
const subject = event.queryStringParameters.name || 'World'
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello ${subject}` }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }
}
}
module.exports = { handler }
When I run deploy the function, life is good!
curl -sL https://classy-cajeta-1f14a7.netlify.app/api/v1/test | jq
{
"message": "Hello World"
}
But locally, I’m having trouble. Hitting the function ignoring the redirect is fine. I start the server with netlify functions:serve --debug --port 8080
:
curl -sL http://localhost:8080/.netlify/functions/main/test | jq
{
"message": "Hello World"
}
But trying the redirect doesn’t work:
curl -sL http://localhost:8080/api/v1/test
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /api/v1/test</pre>
</body>
</html>
Any tips? I do recall that the redirects should work locally but maybe I’m daydreaming. Repo is here:
Thanks!