├── .firebaserc ├── .gitignore ├── README.md ├── firebase.json ├── functions ├── index.js ├── package.json └── views │ ├── index.hbs │ └── partials │ └── partial.hbs └── public └── 404.html /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "your-project-alias" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | functions/node_modules 2 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Firebase NodeJS Starter 2 | 3 | As javascript developers we are always looking to running our NodeJS apps on the server. You have to install nginx to write reverse proxy or any other libraries, configurations for security performance etc. Then what about using firebase hosting features to running your app? Yes it's possible! With Cloud Functions of Firebase, you are able to use power of Google Cloud Services without any extra payment for SSL, security, performance or any other similar topics. 4 | 5 | Here I created this repository to help who wants to write NodeJS apps and deploy it easily without thinking backend parts. You can use this repository for Server Side Rendering with ExpressJS or API server. Ok Lets start! 6 | 7 | ## Before Using It: 8 | I assume you've already installed NodeJS (Version 6+), Firebase Tools, and Git on the machine. 9 | 10 | ## How To Use It? 11 | - First, you have to create a new Firebase app from Firebase Console. 12 | - After creating app, open your terminal and clone this library with these command: 13 | ```sh 14 | $ git clone https://github.com/serdaraglamis/firebase-nodejs-starter ./ 15 | ``` 16 | - Then we have to install our packages 17 | ```sh 18 | $ cd functions 19 | $ npm install 20 | ``` 21 | - After installed all necessary packages. Make this project your own and connect with your created project by typing: 22 | ```sh 23 | $ firebase use --add 24 | ``` 25 | - Its ready you can test it locally by typing (With live reload feature. Thanks to firebase !) 26 | ```sh 27 | $ firebase serve --only functions,hosting 28 | ``` 29 | - Also You can deploy project to live url with typing (It will give your url :) ) 30 | ```sh 31 | $ firebase deploy 32 | ``` 33 | 34 | # How Its Working? 35 | This is an easy Express app using Handlebars to render templates: 36 | ```js 37 | 38 | const functions = require('firebase-functions'); // This is provided by firebase to use its functions 39 | const express = require('express'); // This is standart express import 40 | const firebase = require('firebase-admin'); // Firebase Admin SDK. You can reach all of your data with easily 41 | const engines = require('consolidate'); // One place for express engines :) 42 | const Handlebars = require('handlebars'); // Template viewing engine 43 | const fs = require('fs'); // Used to read directory listings 44 | 45 | // This is for initialize express app 46 | const app = express(); 47 | 48 | // this is for initalize firebase 49 | const firebaseApp = firebase.initializeApp(functions.config().firebase); 50 | 51 | // App view engine setups 52 | app.engine('hbs', engines.handlebars); 53 | app.set('views', './views'); 54 | app.set('view engine', 'hbs'); 55 | Handlebars.registerPartial('partial', fs.readFileSync(__dirname + '/views/partials/partial.hbs', 'utf8')); 56 | 57 | // Express Route - Renders index page 58 | app.get('/', (request, response) => { 59 | // response.set('Cache-Control', 'public, max-age=300, s-maxage=600'); For caching use that 60 | response.render('index', {data: { 61 | author: 'SERDAR AGLAMIS' 62 | }}) 63 | }); 64 | 65 | // Exports app for firebase https requests 66 | exports.app = functions.https.onRequest(app); 67 | ``` 68 | 69 | ### Tech 70 | 71 | This project uses a number of open source projects to work properly: 72 | 73 | * [Express] - Fast, unopinionated, minimalist web framework for Node.js 74 | * [Consolidate] - Template engine consolidation library for node.js 75 | * [node.js] - Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. 76 | * [Handlebars](http://handlebarsjs.com/) - Minimal Templating on Steroids 77 | 78 | And of course this project itself is open source :) 79 | 80 | [node.js]: 81 | [express]: 82 | [Consolidate]: 83 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "public", 4 | "rewrites": [ 5 | { 6 | "source": "**", 7 | "function": "app" 8 | } 9 | ] 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /functions/index.js: -------------------------------------------------------------------------------- 1 | const functions = require('firebase-functions'); 2 | const express = require('express'); 3 | const firebase = require('firebase-admin'); 4 | const engines = require('consolidate'); 5 | const Handlebars = require('handlebars'); 6 | const fs = require('fs'); 7 | 8 | const app = express(); 9 | 10 | const firebaseApp = firebase.initializeApp(functions.config().firebase); 11 | 12 | function getFirebaseData(path) { 13 | const ref = firebase.database().ref(path); 14 | return ref.once('value').then(snap => snap.val()); 15 | } 16 | 17 | app.engine('hbs', engines.handlebars); 18 | 19 | app.set('views', './views'); 20 | app.set('view engine', 'hbs'); 21 | 22 | Handlebars.registerPartial('partial', fs.readFileSync(__dirname + '/views/partials/partial.hbs', 'utf8')); 23 | 24 | 25 | app.get('/', (request, response) => { 26 | // response.set('Cache-Control', 'public, max-age=300, s-maxage=600'); 27 | response.render('index', {data: { 28 | author: 'SERDAR AGLAMIS' 29 | }}) 30 | }); 31 | 32 | app.get('/cached', (request, response) => { 33 | response.set('Cache-Control', 'public, max-age=300, s-maxage=600'); 34 | response.render('index', {data: { 35 | author: 'SERDAR AGLAMIS' 36 | }}) 37 | }); 38 | 39 | exports.app = functions.https.onRequest(app); 40 | -------------------------------------------------------------------------------- /functions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "functions", 3 | "description": "Firebase NodeJS Starter App", 4 | "dependencies": { 5 | "firebase-admin": "~5.2.1", 6 | "firebase-functions": "^0.6.2", 7 | "handlebars": "^4.0.10", 8 | "consolidate": "^0.14.5", 9 | "express": "^4.15.4" 10 | }, 11 | "private": true, 12 | "engines": { "node": "8" } 13 | } 14 | -------------------------------------------------------------------------------- /functions/views/index.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Hello From Firebase 6 | 7 | 8 |

This Page Rendered From Firebase

9 | 10 |

Author : '{{data.author}}'

11 |
12 | {{> partial}} 13 | 14 | 15 | -------------------------------------------------------------------------------- /functions/views/partials/partial.hbs: -------------------------------------------------------------------------------- 1 |
Yes! I am rendered from partial :)
-------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Welcome to Firebase Hosting 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 32 | 33 | 34 |
35 |

Welcome

36 |

Firebase Hosting Setup Complete

37 |

You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!

38 | Open Hosting Documentation 39 |
40 |

Firebase SDK Loading…

41 | 42 | 64 | 65 | 66 | --------------------------------------------------------------------------------