Skip to content

Commit 7623ed6

Browse files
author
Nicolas Garnier
committed
Removing the HTTPS hello world quickstart and adding back the time-server one.
Change-Id: I394919a1eb3bd1a1b11295729cf7f6aad5a895ce
1 parent 7534df6 commit 7623ed6

File tree

6 files changed

+102
-123
lines changed

6 files changed

+102
-123
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ To learn how to get started with the Firebase SDKL for Cloud Functions try the q
1212

1313
This repository contains the following samples:
1414

15-
### [Quickstart: Realtime Database trigger](/quickstarts/uppercase)
15+
### [Realtime database trigger quickstart: Uppercaser](/quickstarts/uppercase)
1616

1717
This quickstart sample demonstrates using **Cloud Functions** with a **Firebase Realtime Database** trigger. The function will uppercase messages written to the datastore.
1818

19-
### [Quickstart: HTTPS trigger](/quickstarts/time)
19+
### [HTTPS trigger quickstart: Time Server](/quickstarts/time)
2020

2121
This quickstart sample demonstrates using **Cloud Functions** triggered by **HTTPS requests**. The function will return the current server time and allows for date time formatting.
2222

23-
### [Quickstart: Firebase Storage Trigger](/quickstarts/thumbnails)
23+
### [Cloud Storage trigger quickstart: Thumbnail generator](/quickstarts/thumbnails)
2424

2525
This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Storage events**. The function will generate a thumbnail of uploaded images.
2626

27+
### [PubSub trigger quickstart: Hello World](/quickstarts/thumbnails)
28+
29+
This quickstart sample demonstrates using **Cloud Functions** triggered by **PubSub events**. The function will log the PubSub payload in a Hello world message.
30+
2731
### [Send FCM notifications](fcm-notifications)
2832

2933
This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification from a Realtime Database triggered Function when users get new followers. The sample also features a Web UI to experience the FCM notification.

quickstarts/https-helloworld/functions/index.js

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Firebase SDK for Cloud Functions Quickstart - HTTPS trigger
22

3-
This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with an HTTPS trigger.
3+
This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with an HTTPS trigger through building an endpoint returning the current time.
44

55

66
## Introduction
77

8-
We'll show a series of basic HTTPS triggered Functions.
8+
The function `date` simply returns the current server date. You can pass it a `format` URL Query parameter to format the date.
99

1010
Further reading:
1111

@@ -45,7 +45,10 @@ First you need to install the `npm` dependencies of the functions:
4545
cd functions && npm install; cd ..
4646
```
4747

48-
This installs locally the Firebase SDK and the Firebase SDK for Cloud Functions.
48+
This installs locally:
49+
- The Firebase SDK and the Firebase Functions SDK.
50+
- The [moment](https://www.npmjs.com/package/moment) npm package to format time.
51+
- The [cors](https://www.npmjs.com/package/cors) npm package to allow Cross Origin AJAX requests on the endpoint.
4952

5053
Deploy to Firebase using the following command:
5154

@@ -60,7 +63,15 @@ This deploys and activate the date Function.
6063

6164
## Try the sample
6265

63-
To try the samples please follow the [HTTPS trigger documentation](https://firebase.google.com/preview/functions/gcp-https)
66+
You can first simply hit the Function URL directly. After deploying the function you can simply open the following URLs in your browser:
67+
68+
```
69+
https://us-central1-<project-id>.cloudfunctions.net/date
70+
71+
https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
72+
```
73+
74+
Formatted dates should be displayed.
6475

6576

6677
## Contributing
File renamed without changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// [START functionsimport]
19+
const functions = require('firebase-functions');
20+
// [END functionsimport]
21+
// [START additionalimports]
22+
// Moments library to format dates.
23+
const moment = require('moment');
24+
// CORS Express middleware to enable CORS Requests.
25+
const cors = require('cors')({origin: true});
26+
// [END additionalimports]
27+
28+
// [START all]
29+
/**
30+
* Returns the server's date. You must provide a `format` URL query parameter or `format` vaue in
31+
* the request body with which we'll try to format the date.
32+
*
33+
* Format must follow the Node moment library. See: http://momentjs.com/
34+
*
35+
* Example format: "MMMM Do YYYY, h:mm:ss a".
36+
* Example request using URL query parameters:
37+
* https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
38+
* Example request using request body with cURL:
39+
* curl -H 'Content-Type: application/json' /
40+
* -d '{"format": "MMMM Do YYYY, h:mm:ss a"}' /
41+
* https://us-central1-<project-id>.cloudfunctions.net/date
42+
*
43+
* This endpoint supports CORS.
44+
*/
45+
// [START trigger]
46+
exports.date = functions.https().onRequest((req, res) => {
47+
// [END trigger]
48+
// [START sendError]
49+
// Forbidding PUT requests.
50+
if (req.method === 'PUT') {
51+
res.status(403).send('Forbidden!');
52+
}
53+
// [END sendError]
54+
55+
// [START usingMiddleware]
56+
// Enable CORS using the `cors` express middleware.
57+
cors(req, res, () => {
58+
// [END usingMiddleware]
59+
// Reading date format from URL query parameter.
60+
// [START readQueryParam]
61+
let format = req.query.format;
62+
// [END readQueryParam]
63+
// Reading date format from request body query parameter
64+
if (!format) {
65+
// [START readBodyParam]
66+
format = req.body.format;
67+
// [END readBodyParam]
68+
}
69+
// [START sendResponse]
70+
const formattedDate = moment().format(format);
71+
console.log('Sending Formatted date:', formattedDate);
72+
res.status(200).send(formattedDate);
73+
// [END sendResponse]
74+
});
75+
});
76+
// [END all]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "https-quickstart-functions",
3-
"description": "HTTPS Firebase Functions Quickstart sample",
2+
"name": "time-server-functions",
3+
"description": "A simple time server using HTTPS Cloud Function",
44
"dependencies": {
55
"cors": "^2.8.1",
6-
"dateformat": "^1.0.12",
76
"firebase": "^3.6",
8-
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
7+
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz",
8+
"moment": "^2.17.1"
99
}
1010
}

0 commit comments

Comments
 (0)