Skip to content

Commit f766f50

Browse files
authored
Add Cloud Functions template render sample. (GoogleCloudPlatform#365)
1 parent 3440412 commit f766f50

File tree

5 files changed

+380
-293
lines changed

5 files changed

+380
-293
lines changed

functions/helloworld/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,15 @@ exports.helloError3 = function helloError3 (event, callback) {
148148
};
149149
// [END functions_helloworld_error_3]
150150
/* eslint-enable */
151+
152+
// [START functions_helloworld_template]
153+
const pug = require('pug');
154+
155+
// Renders the index.pug
156+
exports.helloTemplate = (req, res) => {
157+
// Render the index.pug file
158+
const html = pug.renderFile('./index.pug');
159+
160+
res.send(html).end();
161+
};
162+
// [END functions_helloworld_template]

functions/helloworld/index.pug

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
doctype html
2+
html(lang="en")
3+
head
4+
title= pageTitle
5+
body
6+
h1 Cloud Functions Template Sample
7+
p.
8+
Pug is a terse and simple templating language with a
9+
strong focus on performance and powerful features.

functions/helloworld/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"dependencies": {
2020
"@google-cloud/debug-agent": "1.0.0",
21+
"pug": "2.0.0-beta.12",
2122
"safe-buffer": "5.0.1"
2223
},
2324
"devDependencies": {

functions/helloworld/test/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,17 @@ test.serial(`helloError3: callback shoud return an errback value`, (t) => {
192192
t.deepEqual(callback.callCount, 1);
193193
t.deepEqual(callback.firstCall.args, [expectedMsg]);
194194
});
195+
196+
test.serial(`helloTemplate: should render the html`, async (t) => {
197+
const req = {};
198+
const res = {};
199+
res.send = sinon.stub().returnsThis();
200+
res.end = sinon.stub().returnsThis();
201+
202+
program.helloTemplate(req, res);
203+
204+
t.is(res.send.callCount, 1);
205+
t.is(res.send.getCall(0).args.length, 1);
206+
t.is(res.send.getCall(0).args[0].includes('<h1>Cloud Functions Template Sample</h1>'), true);
207+
t.is(res.end.callCount, 1);
208+
});

0 commit comments

Comments
 (0)