edurouter.js 1.9 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('/insertWork').all((req, res, next) => {
  36. postmysql(req, res, "insertWork");
  37. });
  38. queryString = function(url) {
  39. var a = url.split("?");
  40. var json = querystring.parse(a[1])
  41. return json;
  42. }
  43. //post存儲過程處理
  44. postmysql = function(req, res, functionname) {
  45. //mode的處理
  46. if (req.body[0]) {
  47. p = Object.values(req.body[0]);
  48. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  49. //執行存儲過程
  50. mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); });
  51. }
  52. }
  53. //get獲取數據庫信息
  54. getmysql = function(req, res, functionname, ...p) {
  55. p = p || [];
  56. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  57. //執行存儲過程
  58. mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); }); //
  59. }
  60. module.exports = router;