edurouter.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. router.route('/selectWorkById').all((req, res, next) => {
  43. var json = queryString(req.url);
  44. getmysql(req, res, "selectWorkById", json['id']);
  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) { res.end(JSON.stringify(ret)); }); //
  67. }
  68. module.exports = router;