node.js - Using handlebars in Cloud Functions for Firebase -


i trying develop prototype using firebase.

i using firebase function. package.json file in function directory is:

{   "name": "functions",   "description": "cloud functions firebase",   "dependencies": {     "express": "^4.15.4",     "firebase-admin": "~4.2.1",     "firebase-functions": "^0.5.7",     "hbs": "^4.0.1"   },   "private": true } 

my index.js file looks like:

const functions = require('firebase-functions'); const express = require('express'); const hbs = require('hbs');  var app = express(); app.set('view engine', hbs);  const admin = require('firebase-admin');  admin.initializeapp(functions.config().firebase);  exports.getmessages = functions.https.onrequest((req, res) => {     var abc = admin.database().ref('/messages').on("value", function(snapshot) {         res.send(snapshot.val());       }, function (errorobject) {         console.log("the read failed: " + errorobject.code);       });    });    exports.showmessage = functions.https.onrequest((req, res) => {     res.render('about.hbs' , {       pagetitle : 'about page',       currentyear  : new date().getfullyear()     });   }); 

while "getmessage" function works fine, error while executing "showmessage" function uses third party module hbs.

function logs show

error: cannot find module 'hbs'     @ function.module._resolvefilename (module.js:469:15)     @ function.module._load (module.js:417:25)     @ module.require (module.js:497:17)     @ require (internal/module.js:20:19)     @ new view (/var/tmp/worker/node_modules/express/lib/view.js:50:49)     @ eventemitter.app.render (/var/tmp/worker/node_modules/express/lib/application.js:545:12)     @ serverresponse.res.render (/var/tmp/worker/node_modules/express/lib/response.js:938:7)     @ exports.showmessage.functions.https.onrequest (/user_code/index.js:46:9)     @ cloudfunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)     @ /var/tmp/worker/worker.js:635:7 

what you've done here created new express app , set default view engine hbs:

var app = express(); app.set('view engine', hbs); 

but doesn't cause cloud functions app. app object created sitting there, unused.

if want use express app cloud functions, you'll have pass along onrequest:

exports.app = functions.https.onrequest(app); 

this means you'll have configure endpoints on express app knows url paths service.

if want use handlebars cloud functions, why don't start this sample project firebase team?


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -