Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions quickstarts/time-server/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
*/
'use strict';

// [START functionsimport]
const functions = require('firebase-functions');
// [END functionsimport]
// [START additionalimports]

// Moments library to format dates.
const moment = require('moment');
// CORS Express middleware to enable CORS Requests.
const cors = require('cors')({
origin: true,
});
// [END additionalimports]

// [START all]
/**
* Returns the server's date. You must provide a `format` URL query parameter or `format` value in
* the request body with which we'll try to format the date.
Expand All @@ -44,35 +40,26 @@ const cors = require('cors')({
*
* This endpoint supports CORS.
*/
// [START trigger]

exports.date = functions.https.onRequest((req, res) => {
// [END trigger]
// [START sendError]

// Forbidding PUT requests.
if (req.method === 'PUT') {
return res.status(403).send('Forbidden!');
}
// [END sendError]

// [START usingMiddleware]
// Enable CORS using the `cors` express middleware.
return cors(req, res, () => {
// [END usingMiddleware]

// Reading date format from URL query parameter.
// [START readQueryParam]
let format = req.query.format;
// [END readQueryParam]
// Reading date format from request body query parameter
if (!format) {
// [START readBodyParam]
format = req.body.format;
// [END readBodyParam]
}
// [START sendResponse]

const formattedDate = moment().format(format);
console.log('Sending Formatted date:', formattedDate);
res.status(200).send(formattedDate);
// [END sendResponse]
});
});
// [END all]