Explore Firebase Cloud Functions

Introduction:

Firebase, Backend as a service, released a new feature Cloud Functions. It allows developers write back-end code without worrying handling a server. In the following blog post, I explored how fast it is to create a simple HTTP request listener.

1. Install the Firebase CLI tools:

npm install -g firebase-tools

2. Login with Google account:

firebase login

3. Initialize a Firebase project:

firebase init

4. Select features with number keys [1,2,3]:

1-firebase-init

5. Select Firebase project:

After the initialization, the following files structure will be created:

2-indexjs-get-request.PNG

The Cloud Functions can then be defined in the index.js file. Note that ES6 is used.

6. Deploy the functions:

3-deploy.PNG

7. Test it!

The Firebase console should show the deployed function:

4-firebase-console

And it can be tested via postman:

5-postman-get-request.PNG

8 Different Request Methods

Different HTTP methods (GET POST etc.) of a route can be defined as follows:

6-indexjs-post-request

7-postman-post-request.PNG

9. Refactoring

Having many functions in one index.js file is not desired. Therefore I created Handler classes for handling logic.

8-refactor-file-tree.PNG

Index.js (managing all the routes):

// imports
const functions = require('firebase-functions');
// handlers
const HellowordHandler = require('./handlers/helloword');
const hellowordHandler = new HellowordHandler('DEV');
// routes
exports.helloWorld = functions.https.onRequest(hellowordHandler.handle.bind(hellowordHandler));

helloword.js (the logic in ES6 class):

class HelloWordHandler {
  constructor(env) {
    console.log(`Env: ${env}`);
  }
  handle(request, response) {
    switch (request.method) {
      case 'GET':
        this.getRequest(request, response);
        break;
      case 'POST':
        this.postRequest(request, response);
        break;
      default:
        response.status(403).send('Not support.');
        break;
    }
  }
  getRequest(request, response) {
    response.send('GET request');
  }
  postRequest(request, response) {
    console.log(request.body);
    response.send(request.body);
  }
}
module.exports = HelloWordHandler;

In this way, we can separate different logic to multiple files. In this just my first try to refactor it, if anyone has any suggests, please feel free to leave a comment :D.

Advertisements

2 thoughts on “Explore Firebase Cloud Functions

    1. Sorry for the late reply, I think it can run ES6 directly. If you want more features which are not yet supported by the node natively, such as async await, you can try Webpack, Babel.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s