edurouter.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // ROUTES FOR OUR API
  2. // =============================================================================
  3. var express = require('express');
  4. var bcrypt = require('bcryptjs');
  5. var router = express.Router(); // get an instance of the express Router
  6. const querystring = require('querystring');
  7. var mysql = require('./mysql');
  8. const _mysqlLabor = ["123.58.32.151", "art"]; //edu數據庫信息
  9. // const _mysqlLabor = ["172.16.12.5", "art"]; //edu數據庫信息
  10. //統壹處理區域
  11. router.use(async function(req, res, next) {
  12. await asynnext(req, res, next);
  13. //// make sure we go to the next routes and don't stop here
  14. //res.end("");
  15. });
  16. /**
  17. * @des 參數解釋同上
  18. */
  19. function asynnext(req, res, next) {
  20. //異步處理
  21. return new Promise(async(resolve, reject) => {
  22. next();
  23. //resolve(true);aa
  24. });
  25. }
  26. /*測試*/
  27. // router.route('/a').all((req, res, next) => {
  28. // var json = queryString(req.url);
  29. // getmysql(req, res, "a", json['ph'], json['pa']);
  30. // });
  31. // router.route('/b').all((req, res, next) => {
  32. // var json = queryString(req.url);
  33. // getmysql(req, res, "b", json['ph'], json['pa']);
  34. // });
  35. router.route('/b').all((req, res, next) => {
  36. var json = queryString(req.url);
  37. getmysql(req, res, "a");
  38. });
  39. queryString = function(url) {
  40. var a = url.split("?");
  41. var json = querystring.parse(a[1])
  42. return json;
  43. }
  44. //post存儲過程處理
  45. postmysql = function(req, res, functionname) {
  46. //mode的處理
  47. if (req.body[0]) {
  48. p = Object.values(req.body[0]);
  49. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  50. //執行存儲過程
  51. mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); });
  52. }
  53. }
  54. //get獲取數據庫信息
  55. getmysql = function(req, res, functionname, ...p) {
  56. p = p || [];
  57. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  58. //執行存儲過程
  59. mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); }); //
  60. }
  61. module.exports = router;