// ROUTES FOR OUR API // ============================================================================= var express = require('express'); var bcrypt = require('bcryptjs'); var router = express.Router(); // get an instance of the express Router const querystring = require('querystring'); var mysql = require('./mysql'); // const _mysqlLabor = ["123.58.32.151", "art"]; //edu數據庫信息 const _mysqlLabor = ["172.16.12.5", "art"]; //edu數據庫信息 //統壹處理區域 router.use(async function(req, res, next) { await asynnext(req, res, next); //// make sure we go to the next routes and don't stop here //res.end(""); }); /** * @des 參數解釋同上 */ function asynnext(req, res, next) { //異步處理 return new Promise(async(resolve, reject) => { next(); //resolve(true);aa }); } /*測試*/ // router.route('/a').all((req, res, next) => { // var json = queryString(req.url); // getmysql(req, res, "a", json['ph'], json['pa']); // }); // router.route('/b').all((req, res, next) => { // var json = queryString(req.url); // getmysql(req, res, "b", json['ph'], json['pa']); // }); router.route('/insertWork').all((req, res, next) => { postmysql(req, res, "insertWork"); }); router.route('/selectWork').all((req, res, next) => { var json = queryString(req.url); getmysql(req, res, "selectWork", json['page'], 7); }); router.route('/selectWorkById').all((req, res, next) => { var json = queryString(req.url); getmysql(req, res, "selectWorkById", json['id']); }); queryString = function(url) { var a = url.split("?"); var json = querystring.parse(a[1]) return json; } //post存儲過程處理 postmysql = function(req, res, functionname) { //mode的處理 if (req.body[0]) { p = Object.values(req.body[0]); p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname); //執行存儲過程 mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); }); } } //get獲取數據庫信息 getmysql = function(req, res, functionname, ...p) { p = p || []; p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname); //執行存儲過程 mysql.usselect(p, function(ret) { res.end(JSON.stringify(ret)); }); // } module.exports = router;