// implementation detail relevant whenever code is edited.
'use strict';
// cloud-provider / trigger specifc dependancy relevant only to a single cloud provider.
const functions = require('firebase-functions');
// business logic dependancies relevant when understanding solution details, and when making changes.
const moment = require('moment'); // Moments library to format dates.
const cors = require('cors')({origin: true});// CORS Express middleware to enable CORS Requests.
// debugging comment that is relevant when debugging, understanding solution details, making changes, and implementing or testing.
/**
* Returns the server's date. You must provide a `format` URL query parameter or `format` vaue in
* the request body with which we'll try to format the date.
*
* Format must follow the Node moment library. See: http://momentjs.com/
*
* Example format: "MMMM Do YYYY, h:mm:ss a".
* Example request using URL query parameters:
* https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
* Example request using request body with cURL:
* curl -H 'Content-Type: application/json' /
* -d '{"format": "MMMM Do YYYY, h:mm:ss a"}' /
* https://us-central1-<project-id>.cloudfunctions.net/date
*
* This endpoint supports CORS.
*/
// cloud provider specific(?) trigger interface.
exports.date = functions.https.onRequest((req, res) => {
// Forbidding PUT requests.
if (req.method === 'PUT') { // trigger specific implementation detail statement (cloud provider specific(?)) I only question if it's specific to a cloud provider because it may be standardized by express.
res.status(403).send('Forbidden!'); // a trigger specific function-result (cloud provider specific(?))
}
// Enable CORS using the `cors` express middleware.
cors(req, res, () => { // cloud provider specific (?) also trigger specific
let format = prepare(req);
const businessLogicResult = businessLogic(format)
console.log('Sending Formatted date:', businessLogicResult); // debugging statement
res.status(200).send(businessLogicResult); // trigger specific function result
});
});
function
function prepare(req) {
// Reading date format from URL query parameter.
let format = req.query.format; // trigger specific statement
// Reading date format from request body query parameter
if (!format) { // body statement
format = req.body.format; // trigger specific statement
}
return format;
}
function businessLogic(format = "MMMM Do YYYY, h:mm:ss a") {
return moment().format(format);
}
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Last active
April 26, 2017 18:21
-
-
Save dleonard00/de895996a4af6cfdf195c5b45d017342 to your computer and use it in GitHub Desktop.
https://raw.githubusercontent.com/firebase/functions-samples/master/quickstarts/time-server/functions/index.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment