/express-ts-cloud-functions

express framework in Typescript for firebase cloud functions

Primary LanguageTypeScript

express-ts-cloud-functions

This is a template for firebase cloud functions.

Reference

About

  • Typescrpt
  • Express.js

Requirements

  • Firebase CLI
  • Node 10.17.0
  • tsc

Firebase Project Setting

To run on localhost, you have to setup your firebase project as below.

$ firebase login
$ firebase use "<your project id>"

Then, you can access the functions via this URL.

http://localhost:5000/your-firebase-project-id/your-firebase-project-region/api/userinfo/userinfo

Directory Structure

interfaces

TypeScript Interfaces used globally

middleware

Express middleware created by yourself

models

Database models adjust to table structure

repositories

Database CURD and external API call.

routes

HTTP Request handlers. One endpoint is in one file to divide names for Firebase Cloud Functions.

How to Export router

export const login: IRoutes = {
  name: genFunctionName(__dirname, __filename),
  router
};

You have to export router object with name property. Function name is formed by combination of directory name and file name.

About routing path

router.post('/', (req: Request, res: Response) => {
  // Do Something
});

It is recommended to define path as '/'.
Because all endpoints will be exported to Firebase Cloud Functions indevidually with defferent names.

services

Business Logic

Build Setup

$ cd functions

# install dependencies
$ yarn install

# ts to js
$ yarn build

# run on localhost
$ firebase serve