Redirect to function not working

I’ve got a netlify function that renders out some HTML. I setup a redirect in netlify.toml so if a user visits /product/detail/:slug then the function will get invoked instead:

[build]
  command = "npm run-script build" # the command you run to build this file
  functions = "built-lambda" # netlify-lambda builds to this folder AND Netlify reads functions from here
  publish = "build" # create-react-app builds to this folder, Netlify should serve all these files statically


[[redirects]]
  from = "/product/detail/*"
  to = "/.netlify/functions/ssr/:splat"
  status = 200


[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
  force = false

This works perfectly fine with netlify dev however when deploying I get “Cannot GET” but if I invoke the function normally /.netlify/functions/ssr/:slug it works fine

Here’s some links:
https://5db16eb88b4fa30007bfe0a5--peaceful-mccarthy-a7efcd.netlify.com/product/detail/chanel-255-wallet-on-chain-a70328-y83883-c3906-974-bf59-4ec6-b19f-7d4eb51c9b1a/

(README: Run this with javascript disabled, internal routing takes over and routes somewhere else)

1 Like

I figured out what the issue was.

For anybody interested, I was using express on my serverless function and I was using router instead of app.get for my routes.

I initialised the router like so: app.use('/.netlify/functions/ssr/', router) so when it was being called from another URL it wasn’t working.

2 Likes

Thanks so much for posting that followup, Danny! I didn’t know about that trick so I am doubly glad to hear you found a solution :slight_smile:

How do you fix the problem? Are you using the other syntax or how do you get around?

Hey, it was an issue with my implementation

I was using express in the lambda function and I was routing incorrectly

Let me know if you need more help

As I’m still using express with lambda functions, how do you correct your routing url to use it with express ?

Any code example would be appreciate :slight_smile:

This is how I have it setup:

const router = express.Router();
router.get('*/:id', (req, res) => {
// Code goes here
});

app.use(bodyParser.json());
app.use('/', router); 
1 Like

Thank you so much Danny ! That’s help me solved mine too :slight_smile:

1 Like

thanks for sharing your solutions, folks! love to see people helping each other :muscle: