I’m looking for separating the repository of the functions (backend) and the site (frontend). Therefore, there’s no site beside of the functions and Netlify tries to create a new one automatically. I’m planning to create a Rest API backend only with Netlify Functions. How should I configure Netlify Functions to achieve that?
Note: I use TypeScript, and the built .netlify directory looks like this: .netlify/functions-serve/test
The short answer is: you can’t do this with Netlify. You can only route to functions based on path. There is no way to route to functions based on HTTP method.
You basically have three options:
Create a single monolith function with a router
Create a function for each path, each with a little mini-router based on HTTP method
Don’t build a REST API
If you decide to go option two, the netlify.toml file can be used to create some limited redirects. For example, here is some code from a project of mine:
[[redirects]]
from = "/api/games/:gameId/"
to = "/.netlify/functions/fetch-game/:gameId"
query = { latest = ":latest" }
status = 200
[[redirects]]
from = "/api/games/:gameId/"
to = "/.netlify/functions/fetch-game/:gameId"
status = 200
This redirects /games/:gameId requests to my fetch-game.js function. This includes requests both with and without the optional latest query parameter. The 200 status makes the redirect seamless. It will look to clients as if they are just sending a request to /games/:gameId.
Of course, any request sent to /games/:gameId will be redirected: GET, POST, PUT, etc. As it so happens, GET is the only valid method for this route in my API. So while fetch-game.js does not include a mini-router, it does have HTTP method validation, and will send back a 404 if the method is not GET.
In the future, it is possible you could use Netlify’s Edge Handlers to route based on HTTP method. However, that feature is still in beta.