As a developer, I often need to run a set of several tasks happened when certain event occurs. For example:

Definitely, this is not an issue when all this functionality can be added to the core functionality of the app/or website. However, in some cases, it's not possible to extend core functionality of the app, and making another standalone application just to handle contact form does not make sense (especially, if your client needs a cost-effective solution and our time is limited).

Let's say, we have a client who have a static HTML website, and wanted to add a simple contact form, which just sends an email when someone submits the form.

Of course, there are number of services, like Formspree, which we can use for this task. However, functionality of such services is limited, so if later our client want to receive notifications to the Slack channel, we'll need to find a way to connect this service with Slack.

So, how we can solve this issue and offer cost-effective and easy to use solution to our customer? The answer is - use serverless approach! Serverless functions are great for such tasks, as they provide the following benefits:

As an example, I wrote simple function which could be hosted on Cloudflare Workers. That function will listen for incoming POST request, validates incoming form data, and if everything is correct, sends an email to website admin and email confirmation to the sender:

Then, we just need to add a quick javascript code to the client's website which will send the form data via AJAX to Worker's URL:

You can also take a look a t live demo on Sandbox.

A few notes:

This function could be hosted on any other serverless service provider, for example - AWS Lambda. However, in my opinion, Cloudflare Workers - is a great service to host small serverless functions, while AWS Lambda requires more initial setup and configuration.

Share this post:
Comments or questions? Shoot me a tweet!