cloud.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // ROUTES FOR OUR API
  2. // =============================================================================
  3. var express = require('express');
  4. var router = express.Router(); // get an instance of the express Router
  5. const querystring = require('querystring');
  6. var mysql = require('./mysql');
  7. const _mysqlLabor = ["172.16.12.5", "cuhkjc_query"]; //query數據庫信息
  8. //統壹處理區域
  9. router.use(async function (req, res, next) {
  10. await asynnext(req, res, next);
  11. });
  12. /**
  13. * @des 參數解釋同上
  14. */
  15. function asynnext(req, res, next) {
  16. //異步處理
  17. return new Promise(async (resolve, reject) => {
  18. next();
  19. //resolve(true);
  20. });
  21. }
  22. //指定用户创建云端事件的行为
  23. router.route('/event_create').post((req, res, next) => {
  24. postmysql(req, res, "cloud_event_create");
  25. }).get((req, res, next) => {
  26. getmysql(req, res, "cloud_event_create_get")
  27. });
  28. //指定用户删除云端事件的行为
  29. router.route('/event_del').post((req, res, next) => {
  30. postmysql(req, res, "cloud_event_del");
  31. }).get((req, res, next) => {
  32. getmysql(req, res, "cloud_event_del_get")
  33. });
  34. //指定用户创建云端可视化面板的行为
  35. router.route('/panel_create').post((req, res, next) => {
  36. postmysql(req, res, "cloud_panel_create");
  37. }).get((req, res, next) => {
  38. getmysql(req, res, "cloud_panel_create_get")
  39. });
  40. //指定用户删除云端可视化面板的行为
  41. router.route('/panel_del').post((req, res, next) => {
  42. postmysql(req, res, "cloud_panel_del");
  43. }).get((req, res, next) => {
  44. getmysql(req, res, "cloud_panel_del_get")
  45. });
  46. queryString = function (url) {
  47. var a = url.split("?");
  48. var json = querystring.parse(a[1])
  49. return json;
  50. }
  51. //post存儲過程處理
  52. postmysql = function (req, res, functionname) {
  53. //mode的處理
  54. if (req.body[0]) {
  55. p = Object.values(req.body[0]);
  56. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  57. //執行存儲過程
  58. mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); });
  59. }
  60. }
  61. //get獲取數據庫信息
  62. getmysql = function (req, res, functionname, ...p) {
  63. p = p || [];
  64. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  65. //執行存儲過程
  66. mysql.usselect(p, function (ret) {
  67. res.end(JSON.stringify(ret));
  68. }); //
  69. }
  70. module.exports = router;