DEV Community

Cover image for Going serverless using Cloudflare Workers
TD
TD

Posted on

Going serverless using Cloudflare Workers

Over the last two weeks, I have worked with my peers on a project called My Photohub. A project being put together by student developers at Seneca College.

Initial Design

Initially, we had an Express backend for my PR to process form submissions. However, we wanted to eliminate the need for maintaining and configuring the backend on our own. Therefore, we decided to go serverless, using Cloudflare Workers.

Going Serverless

Using a serverless approach could be problematic for someone who has been used to setting up a server for the backend. However, I challenged myself to get familiar with Cloudflare Workers to the point that I could have a serverless backend demo to present to my team members.

Doing Research

The first step was to do my research.

I first read the documentation on the Cloudflare website and learned what precisely serverless computing is. I learned that serverless computing does not mean that there would be no servers involved. We, as developers, would not have to worry about maintaining the servers since Cloudflare would manage them for us.

But why Cloudflare Workers?

One may still wonder why Cloudflare Workers when I can easily create a server using Node.js.

Top three reasons to use Cloudflare Workers:

  • Reduced costs (you only pay for what you use)
  • Eliminates the need to manage server
  • Easily scalable

Writing the Code

It took me a couple of days to figure out how to rewrite the current code using syntax for a service worker. I tried several approaches, some worked, and some did not. The tutorials and examples sections of the Cloudflare website are regularly updated to help newcomers get started, and I found them very helpful. The tutorials are well-crafted and can be filtered based on experience level. The beginner tutorials seemed appropriate, as I needed to gain experience with Cloudflare Workers.

Problem Encountered

I overlooked some problems in my implementation, but my teammates caught them. For example, running the service worker through ESLint with incorrect configuration yields errors.
We have to add the following to eslintrc.json file:

  "env": {
    "serviceworker": true
  },
Enter fullscreen mode Exit fullscreen mode

Improving the Code

David immensely helped me improve my code through his constructive feedback on the PR. His comments helped me remove redundancies and restructure the code so it's cleaner.

Final Thoughts

Technology is an ever-evolving thing. As developers, we have to keep up with it by expanding our skills and knowledge portfolio. I also learned that with every revision, I improved my code.

Top comments (0)