index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // [START functionsimport]
  2. const functions = require('firebase-functions');
  3. const express = require('express');
  4. const mock = require('./mock/index');
  5. const app = express();
  6. const sendData = (body, req, res) => {
  7. if (!body) {
  8. res.send('test');
  9. return '';
  10. }
  11. if (typeof body === 'function') {
  12. body(req, res);
  13. }
  14. res.send(body);
  15. };
  16. app.get('/api', (req, res) => {
  17. const html = Object.keys(mock).map(url => {
  18. const href = url.split(' /')[1];
  19. return `<li><a href="${href}"><code>${url}</code></a></li>`;
  20. });
  21. res.send(`<ul>${html.join('')}</ul>`);
  22. });
  23. app.get('/', (req, res) => {
  24. res.send(`<ul><li><a href="api/api"><code>/api</code></a></li></ul>`);
  25. });
  26. Object.keys(mock).forEach(url => {
  27. const body = mock[url];
  28. const urlParams = url.split(' ');
  29. const path = urlParams[1];
  30. const send = (req, res) => {
  31. sendData(body, req, res);
  32. };
  33. if (urlParams[0] === 'GET') {
  34. app.get(path, send);
  35. }
  36. if (urlParams[0] === 'POST') {
  37. app.post(path, send);
  38. }
  39. });
  40. exports.api = functions.https.onRequest(app);