edurouter.js 2.1 KB

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