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:

  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

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

  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:

(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.


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('/', 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: