Firebase Function Sample
程序员文章站
2022-07-13 18:05:43
...
Sample for Express
Sample Code2
var functions = require("firebase-functions"); const express = require("express"); //Set up ExpressJS Server const expressRouter = new express.Router(); //expressRouter.get('*', (req: any, res: { send: (arg0: string) => void; }) => { // res.send(`Hello *`); //}); expressRouter.get('/Test/:id', (req: any, res: any) => { //functions.console.log(req.params.id); res.send(`Hello Test`); }); expressRouter.get('/GetVersion', (req: any, res: any) => { res.send(`Hello GetVersion`); }); expressRouter.get('/*', (req: any, res: any) => { res.send(`NULL`); }); //Cloud Function exports.express = functions.https.onRequest(expressRouter);
Sample Code2
var functions = require("firebase-functions"); const express = require('express'); const cors = require('cors'); const app = express(); //Init the session to the target firbease project var admin = require("firebase-admin"); var serviceAccount = require("../Res/multimanagementsystem.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: `https://${serviceAccount.project_id}-default-rtdb.firebaseio.com` }); const db = admin.firestore(); // Automatically allow cross-origin requests app.use(cors({ origin: true })); // Add middleware to authenticate requests //app.use(myMiddleware); //Added by Jacky const strVerion = "01.0001.20210315"; // app.get('/getCurVersion', (request, response) => { response.json( {version:strVerion} ); }) // app.get('/getUser', async(request, response) => { response.json( {version:strVerion} ); }) app.get('/:id/:name', async(request, response) => { functions.logger.info(request.params.id); functions.logger.info(request.params.name); const user = db.collection('users').doc(request.params.id); const doc = user.get(); if (!doc.exists) { console.log('No such document!'); } else { console.log('Document data:', doc.data()); } response.json({version:strVerion}); }); /* router.get('/getUser/:id', (req, rsp)=>{ functions.logger.info(req.params.id); const user = db.collection('users').doc(req.params.id); const doc = user.get(); if (!doc.exists) { console.log('No such document!'); } else { console.log('Document data:', doc.data()); } rsp.json({version:strVerion}); }); */ async function writeDBFunction() { const docRef = db.collection('users').doc('alovelace'); await docRef.set({ first: 'Ada', last: 'Lovelace', born: 1815 }); } app.get('/writeDB', async(request, response) => { writeDBFunction(); response.send("Succeed to write DB!"); }); async function readDBFunction() { const snapshot = await db.collection('users').get(); functions.logger.info(snapshot, {structuredData: true}); } app.get('/readDB', async(request, response) => { readDBFunction(); response.send("Succeed to read DB!"); }); // build multiple CRUD interfaces: app.get('/:id', (req, res) => { res.send(req.params.id); }); app.get('/', (req, res) => { res.send('list()'); }); app.get('/Test/:id', (req, res) => { //functions.console.log(req.params.id); res.send(`Hello Test`); }); app.get('/GetVersion', (req, res) => { res.send(`Hello GetVersion`); }); app.get('/*', (req, res) => { res.send(`NULL`); }); /* app.post('/', (req, res) => { res.send('create()'); }); app.put('/:id', (req, res) => { res.send(req.params.id + req.body); }); app.delete('/:id', (req, res) => { res.send(req.params.id); }); */ // Expose Express API as a single Cloud Function: // exports.managmentapi = functions.https.onRequest(app); export const mainCloudFunctions = functions.region("europe-west3"); //Set region if necessary export const managmentapi = mainCloudFunctions.https.onRequest(app);
{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017", "noImplicitAny": false }, "compileOnSave": true, "include": [ "src" ] }
推荐阅读
-
php运行出现Call to undefined function curl_init()的解决方法
-
php通过function_exists检测函数是否存在的方法
-
IIS下PHP连接数据库提示mysql undefined function mysql_connect()
-
详解Vue.js搭建路由报错 router.map is not a function
-
【浅析C++11】std::function和std::bind
-
function类型解析
-
Fatal error: Call to undefined function curl_init()解决方法
-
Function eregi is deprecated (解决方法)
-
Function,Object.creat();
-
Arguments Optional 计算两个参数之和的 function