Trying to redirect from /api/endpoint to /.netlify/functions/servers/endpoint

I’ve been trying for a while now to setup my current react website and backend with Netlify.

I was able to setup the lambda functions and they are currently working if I use for example

However, the current website and mobile app currently use /api/latest_netflix for that, so I’ve been trying to setup that redirect in my netlify.toml file, but for some reason I just can’t seem to get it working

this is my netlify.toml file I’ve been trying

command = “npm install && npm run build”
functions = “functions/”

from = “/api/*”
to = ‘/.netlify/functions/servers/:splat’
status = 200
query = {path = “:path”} # apply this rule for /old-path?path=example`

this is how I setup this api endpoint in servers.js

`const express = require(‘express’);
const serverless = require(‘serverless-http’);
const app = express();
const axios = require(‘axios’);
const bodyParser = require(‘body-parser’);
const router = express.Router();

// create a GET route
router.get(’/latest_netflix’, (req, res) => {
//do stuff

app.use(’/.netlify/functions/servers’, router ); // path must route to lambda

module.exports = app;
module.exports.handler = serverless(app);`

the current endpoint works at

Hi, @ntoporcov. I’m seeing this in the most recent deploy details:

No redirect rules processed

So, I think the redirects are not working because the netlify.toml file isn’t found. I also don’t see log lines similar to this for that deploy:

11:03:30 PM: Found Netlify configuration file netlify.toml in site root
11:03:30 PM: Found Netlify configuration file(s). Overriding site configuration

Would you please confirm that netlify.toml is part of this Git repo and that it is located in the base directory of the repo?

I was trying to set it up on a different branch and since then I made updates to the master branch which resulted in auto deploys here. I turned that off since then.

The latest one for my Deploy Preview had that line

I published that deploy just now. Netlify does find the file and process it. The deploy summary even lists one redirect found.

Hi, @ntoporcov, I spot checked a few deploys and I’m looking at either the wrong deploys or the wrong site.

Would you please send me a link to that deploy?

If so, I’d be happy to see what the redirect defined is and if it is working or not. If it isn’t working, I’ll find out why and offer suggestions to (hopefully) get it working as required (or file an issue if the root cause is a “bug”).

This is the entire page of the deploy that I currently have published for

where /.netlify/functions/servers/latest_netflix works but /api/latest_netflix doesn’t

And this is the netlify.toml file for that committ

  command = "npm install && npm run build"
  functions = "functions/"
  from = "/api/*"
  to = "/.netlify/functions/servers/:splat"
  status = 200
  query = {path = ":path"} #  apply this rule for /old-path?path=example

The redirect you have created will ONLY redirect in case you have a query string param of ?path=something. Is that not what you’re seeing?

Not really.

Doesn’t work whereas

Does work

You are seeing exactly what I described; that’s working as expected.

does NOT have a query string value called “path”, which is, as I tried to explain, required for your redirect to work. This works:

@luke @fool I’m facing a similar issue. How can I fix this ? What should be the route in express (should they prepend with .netlify/functions/server) ? And what should be the redirect rule if I’ve to rewrite all requests to my express app ?
My repository is this.

Hi, @rajkumaar23, the redirect rule used is correct:

  from = "/*"
  to = "/.netlify/functions/server/:splat"
  status = 200
  force = true

I don’t know what kind of input this API expects so I only tested the redirect itself. I see this rule above is active and working.

I do notice that the proxy redirected API calls result in 502s errors while the non-proxied API calls returns 404s.

If there are other questions about this, please let us know.

Thanks for the response.
Okay, yeah so the redirect started working when I made few changes in the express code, i.e I changed app.use('/', router) to app.get('/', handler).

Hi, @rajkumaar23, thank you for both letting us know you found a solution and for sharing some details about that solution. This will hopefully help others searching this site in the future. :smiley: