In this article I will describe “on-the-fly” upload of a binary image, using Fastify and Cloudinary in an Node.js environnement.

Image for post
Image for post

Context

Fastify is a high performance web framework built for Node.js. In my opinion it is the best web framework nowadays (as I like to call it “Express 2.0”) for backend purposes.

Cloudinary is a Content Delivery Network, which allow us to perform file upload and storage in a very efficient way. I like it a lot because of all the features around pictures manipulations, and because it provide very good package for free users.

The whole reason when using a CDN is to avoid storing files in your backend storage (wether it is database or file system), because it can lead to performance issues or storage overflow. This is the reason why I implemented a quick way to Upload image from a distant device to a CDN without storing any file. …


Our example will solve the problem of authentication with multiple providers : standard auth, Facebook, Google or anything else. To keep it simple, we will focus only on the login feature.

Setting up the providers

When injecting dependencies, we will create an interface that will define the beans to inject. Here, we will call this interface ILoginProvider : https://gist.github.com/b2cecf9f9b4bd0e202d48bffeec98b21

The login function is where the login logic happens, the supports function is in charge of returning if the AuthRequest object is supported by each provider or not. We pass an object AuthRequest as an argument for login and supportsfunction: https://gist.github.com/c9ebe65da13b05476cb3dcafe7ef1b48

This interface will be implemented by a new class for each source. For our example we will create EmailLoginProvider and FacebookLoginProvider (example below), that will both implement ILoginProvider


Background story

When you are working as a developer for a company, and you want to keep working on side-projects at the same time, you will probably struggle finding a well-known and desirable resource : time. Directly linked to time, another important resource is energy. It is necessary, in my opinion, to save free time for non work-related tasks, in order to relax and maintain enough energy. Finally, the third resource is attention, when a big task is ahead of you, it gets harder to maintain focus.

I mentioned time, energy and attention for a reason : those are often quoted as the components of the TEA framework. The idea behind TEA is that if you lack of any of the 3 resources, you will never be able to achieve optimal productivity. In the case of side-projects, I think of “productivity” not as the amount of code you can deliver, but as the good balance that will provide pride, fulfillment and blooming from working on your side-projects. …

About

Jérémy Basso

Software Engineer @APPSTUD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store