Functions-samples: Using templating engine (Pug) with Cloud Functions

Created on 7 Apr 2017  路  4Comments  路  Source: firebase/functions-samples

Is it possible to use Pug (nodejs tempalting engine) with firebase cloud functions? I've added and installed the node dependency for Pug. I'm trying to open the template file which is in the a directory views(/functions/views), but deploying the function fails with the message no such file or directory, open './views/confirm_email.pug'

How do I open a file in my cloud function's index.js (/functions/index.js) ?

Most helpful comment

thank you for your great question.

    const functions = require('firebase-functions');                                
    const express = require("express");         
    const app = express();


    app.set("views",__dirname+"/tmp");
    app.set("view engine","pug");
    app.engine('pug', require('pug').__express);
    app.get("/",p3p(p3p.recommended),function(req,res){
       res.render("index");
    });
    app.get("/login",p3p(p3p.recommended),function(req,res){
       res.render("login");
    });
    exports.main = functions.https.onRequest(app);

And then you can access that to link like this .
https://us-central1-[YOURAPP].cloudfunctions.net/main/
https://us-central1-[YOURAPP].cloudfunctions.net/main/login

All 4 comments

I had an issue similar to this and I used path.join(__dirname, 'functions/views') and it worked.

I didn't try referencing the path from my cwd, but that might work also... fs.readFileSync('./functions/views/confirm_email.pug')

thank you for your great question.

    const functions = require('firebase-functions');                                
    const express = require("express");         
    const app = express();


    app.set("views",__dirname+"/tmp");
    app.set("view engine","pug");
    app.engine('pug', require('pug').__express);
    app.get("/",p3p(p3p.recommended),function(req,res){
       res.render("index");
    });
    app.get("/login",p3p(p3p.recommended),function(req,res){
       res.render("login");
    });
    exports.main = functions.https.onRequest(app);

And then you can access that to link like this .
https://us-central1-[YOURAPP].cloudfunctions.net/main/
https://us-central1-[YOURAPP].cloudfunctions.net/main/login

Looks like this is resolved. There is also a way to map Firebase hosting URLs to a cloud function. See : https://firebase.google.com/docs/hosting/functions

And we have a sample showing how to use Handlebars templating engine with a Cloud function: https://github.com/firebase/functions-samples/tree/master/template-handlebars

Awesome! Cool stuff indeed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IchordeDionysos picture IchordeDionysos  路  3Comments

MalikFaizanZafar picture MalikFaizanZafar  路  4Comments

motss picture motss  路  4Comments

phuduong060196 picture phuduong060196  路  4Comments

inglesuniversal picture inglesuniversal  路  5Comments