Skip to content

Commit 478daa7

Browse files
committed
Add Cloud Functions template render sample.
1 parent bd96cde commit 478daa7

File tree

5 files changed

+233
-22
lines changed

5 files changed

+233
-22
lines changed

functions/helloworld/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,15 @@ exports.helloError3 = function helloError3 (event, callback) {
146146
};
147147
// [END functions_helloworld_error_3]
148148
/* eslint-enable */
149+
150+
// [START functions_helloworld_template]
151+
const pug = require('pug');
152+
153+
// Renders the index.pug
154+
exports.helloTemplate = (req, res) => {
155+
// Render the index.pug file
156+
const html = pug.renderFile('./index.pug');
157+
158+
res.send(html).end();
159+
};
160+
// [END functions_helloworld_template]

functions/helloworld/index.pug

+9
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
},
2323
"dependencies": {
2424
"@google-cloud/debug-agent": "1.0.0",
25+
"pug": "2.0.0-beta.12",
2526
"safe-buffer": "5.0.1"
2627
},
2728
"devDependencies": {
28-
"@google-cloud/nodejs-repo-tools": "1.3.1",
29+
"@google-cloud/nodejs-repo-tools": "1.3.2",
2930
"ava": "0.19.1",
3031
"proxyquire": "1.7.11",
3132
"sinon": "2.1.0"

functions/helloworld/test/index.test.js

+14
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)