Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const functions = require('firebase-functions');
const dateFormat = require('dateformat');
const cors = require('cors')({origin: true});
// [END import]
// [START all]
/**
* Returns the server's date. A timestamp is returned by default unless a `format` URL query
* parameter is specified with which we'll try to format the date.
*
* Format must follow the Node dateformat library. See: https://www.npmjs.com/package/dateformat
*
* Example format: "yyyy-mm-dd h:MM:ss".
*/
// [START trigger]
exports.date = functions.https().onRequest((req, res) => {
// [END trigger]
cors(req, res, () => {
try {
// [START readQueryParam]
const format = req.query.format;
// [END readQueryParam]
// [START send]
const now = Date.now();
if (format) {
const formattedDate = dateFormat(now, format);
console.log('Sending Formatted date:', formattedDate);
res.send(formattedDate);
} else {
console.log('Sending Timestamp:', now);
res.send(now.toString());
}
import functions from 'firebase-functions';
import admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
/**
* @example POST /addText?text=${text}
*/
export const addText = functions.https.onRequest((req, res) => {
const { text } = req.query;
admin.database()
.ref('/messages')
.push({ text })
.then(() => {
res.status(200).json({ message: 'Text saved.' });
});
});