What is a serverless function?

A serverless function is a small piece of server code that a hosting platform runs on demand for each request, so you do not manage a server yourself.

You write one function that takes a request and returns a response, and upload it. The platform starts it when a request arrives, runs as many copies as needed, and stops it afterward. There is still a server, but you do not set it up or keep it running. The exact shape of the function differs between hosts.

People often use one as a form handler: the form posts to the function, which checks the fields and sends an email. That works, but you then own the code, the email service account and its keys, the spam filtering and the storage.

A form backend replaces that function for the common case. You can still pair the two: let the backend receive and store the submission, then have it call your function through a webhook when you need custom logic.

javascript
export default async function handler(request) {
  const data = await request.formData();
  const email = data.get('email');
  // save it or send an email here
  return Response.redirect('https://example.com/thanks', 303);
}

Related