Backend For Frontend - Using Node.js
Tags: javascript, node, backend, tutorial
Published: 2020-02-08 05:34:17

![BFF](https://miro.medium.com/max/2372/1*xi1CWtBDQh_OyUpEEGawVg.png)

> Super fast client application is the need of current digital era, personalisation in the client application is icing on the cake.


At [Goibibo](https://www.goibibo.com/), Hotels Business is very much focussed, as much as we care about customers’ experience for booking the hotel on our B2C platform, we also care about the experience for the hoteliers while using our platform to manage their Hotel.

For Hoteliers we have a very vast application to manage their property, the application is built using React.js at frontend and Django at backend and it is well known as Extranet in Hotels domain.

---------

## Challenges

We follow micro-service architecture at the core and all the client applications catering hoteliers, directly get data from those micro-services, this started influencing engineers to debate on where to keep the representation logic like, if we keep it at frontend then all the client applications (web, iOS, android and mobile-web) have to write the same logic 3 times, and keeping it in backend is never a good idea. Moreover, getting data from more than one service is again an overhead for client apps and it also exposes all backend services to client applications, that is why we need a Backend For Frontend or BFF.

#### UI processing on Client
Most of the UI processing logic resides on the client, so there is always chance of duplicity in the UI rendering logic for different screen size client application.

#### The Payload Variance
For different screen size clients we can keep the payload size as per the client requirement, which will eventually help in optimising system performance.

#### Collective Response
Different micro services, returns response as per their implementation, so client application needs to process that response every time as per their requirement.

----

## BFF implementation

![Implementation](https://miro.medium.com/max/1284/1*M09G7MHjvfrQdO4LFRNzPA.png)


BFF is again a micro service, which listens to client request and figures out from which backend micro-service it has to fetch the data to serve client application.

We have implemented BFF in Express.js, we chose express because of default non-blocking IO behaviour, following is the tech stack:

* __Express.js__ — Server
* __Mongo DB__ — For caching API Data
* __PM2__ — Process Manager
* __Redis__ — For caching user token

----


## BFF Responsibilities

![Defining Responsibilities](https://miro.medium.com/max/1224/1*q0FVsiVRQ-89yaJ2xfKPnA.jpeg)

BFF act as the main source of truth for all the frontend clients, following the major responsibilities for BFF layer:

* __Authenticated response__ for client side applications
* __Merge response__ from various backend services and provide the response as per the client requirement
* __Trim response__ as per client app requirement, dynamic pagination
* __Support GRPC__ enabled micro-services by exposing http APIs as proxy
* __Caching frequent__ static API calls
* __Personalisation__ for rendering selective UI for the varied sized client application
* __Multi language support__ for different client applications
Support Client-Server Socket connection for realtime updates