Firebase Part II: using express

30th Sep 19
clocks

Here I will be using express to enforce my routes.

Currently you can send a Post request to my GET request and visa versa.  With express I can easily enforce my routes.  I import it with npm and require express within my index page.  

const express = require('express');
...
const app = express();

Then I covert my GET route to use Express. (I will do the same with my POST request)

app.get('/bonsais', (req, res) => {
    admin.firestore().collection('bonsais').get()
    .then(data => {
        let bonsais = [];
        data.forEach(doc => {
            bonsais.push(doc.data())
        });
        return res.json(bonsais);
    })
    .catch(err => console.error(err));
})

I will create an '/api' prefix for my routes:

exports.api = functions.region('europe-west1').https.onRequest(app);

 

I have recently completed my apprenticeship.  I am currently building the API to a React JS site using Firebase.