Giter Site home page Giter Site logo

mayurc137 / serverless-rest-api-firebase-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 47 KB

Building serverless rest api's with Firebase Cloud Functions and Express

JavaScript 100.00%
firebase cloud-functions-for-firebase serverless-architectures rest-api

serverless-rest-api-firebase-boilerplate's Introduction

Building Serverless RESTful Web APIs with Cloud Functions, Firestore, Express and JavaScript ES6

You will need a Firebase project and firebase tools cli

npm install -g firebase-tools

Clone this repository

git clone [email protected]:mayurc137/serverless-rest-api-boilerplate.git .

Updating firebase project id

You need to change the firebase project name in .firebaserc file.

{
  "projects": {
    "default": "YOUR-FIREBASE-PROJECT-ID"
  }
}

After that, you can log in to firebase in the terminal

firebase login

Deploy to firebase

To deploy functions to firebase

firebase deploy

Read values from the request

Content Type\n Request Body\n Behavior\n

application/json	
'{"name":"John"}'	
request.body.name equals 'John'
application/octet-stream	
'my text'	
request.body equals '6d792074657874' (the raw bytes of the request; see the Node.js Buffer documentation)
text/plain	
'my text'	
request.body equals 'my text'
application/x-www-form-urlencoded	
'name=John'	
request.body.name equals 'John'

Adding Middleware

Add middleware to authenticate requests or perform additional tasks

let myMiddleware = (req, res, next) => {
    //authentication code goes here
    next();
}

app.use(myMiddleware);

Building multiple CRUD interfaces:

GET /

app.get('/', (req, res) => {
    let response = Endpoints.list();
    res.send(response);
});

GET /:id

app.get('/:id', (req, res) => {
    let id = req.params.id;
    let response = Endpoints.getById(id);
    res.send(response);
});

POST /

app.post('/', (req, res) => {
    let response = Endpoints.create();
    res.send(response);
});

PUT /:id

app.put('/:id', (req, res) => {
    let id = req.params.id;
    let body = req.body;
    let response = Endpoints.update(id, body);
    res.send(response);
});

DELETE /:id

app.delete('/:id', (req, res) => {
    let id = req.params.id;
    let response = Endpoints.delete(id);
    res.send(response);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.