require('dotenv').config(); const express = require('express'); var bodyParser = require('body-parser'); const server = express(); const pg = require('pg'); //---------read sql files into variables---------------- var fs = require('fs'); var readline = require('readline'); //------------------------------------------------------- var Postgres = new pg.Client({ user: process.env.user, password: process.env.password, host: process.env.host, port: process.env.port, database: process.env.database, ssl: false, application_name: "osm_api" }); Postgres.connect(); Postgres.FirstRow = function (inSQL, args, inResponse) { Postgres.query(inSQL, args, (err, res) => { if (err === null) { inResponse.json(res.rows[0]); return; } console.log(err.stack); inResponse.json(err.stack); }); }; server.get('/', (req, res) => res.send('node.js express is up and running')) server.get('/test_sql', function(req, res){ var path = './route_meta/scenario_package.sql' var callback = function(arg){ res.send(arg) }; fs.readFile(path, 'utf8', function(err, data){ if (!err){ callback(data); } else { callback(err); } }); }); server.get('/get_pool', bodyParser.json(), function (req, res) { var sql = ""; var args = [req.body.quota_rep]; var path = './route_sql/get_pool.sql'; var callback = function(arg){ sql = arg; console.log(req.body.quota_rep); sql = sql.replace("rep_replace",req.body.quota_rep); Postgres.FirstRow(sql,[],res) }; fs.readFile(path, 'utf8', function(err, data){ if (!err){ callback(data); } else { console.log("fatal error pulling sql file") callback(err); } }); }) server.get('/scenario_package', bodyParser.json(), function (req, res) { var sql = ""; var w = ""; var c = 1; var args = []; var path = './route_sql/scenario_package.sql'; var callback = function(arg){ sql = arg; console.log(req.body); //parse request body into a where clause for (var i in req.body) { if (c > 1) { w = w + ` AND ` } w = w + i + " = '" + req.body[i] + "'"; args.push(req.body[i]); c = c + 1; }; //if there was no body sent, return with nothing if (c == 1) { res.send("no body was sent"); return; } //parse the where clause into the main sql statement sql = sql.replace("'where_clause'",w) //execute the sql and send the result Postgres.FirstRow(sql,[],res) }; fs.readFile(path, 'utf8', function(err, data){ if (!err){ callback(data); } else { console.log("fatal error pulling sql file") callback(err); } }); }) server.get('/addmonth_v', bodyParser.json(), function (req, res) { var sql = ""; var w = ""; var c = 1; var args = []; var path = './route_sql/addmonth_vd.sql'; var callback = function(arg){ sql = arg for (var i in req.body.scenario) { //console.log(i); ///console.log(req.body[i]); if (c > 1) { w = w + ` AND ` } w = w + i + " = '" + req.body.scenario[i] + "'"; args.push(req.body.scenario[i]); c = c + 1; }; if (c == 1) { res.send("no body was sent"); return; } console.log("volume adjust:"); console.log(req.body); //console.log(args); sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w); sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount); sql = sql.replace(new RegExp("target_month",'g'),req.body.month); console.log(sql); //console.log(sql); Postgres.FirstRow(sql,[],res) } fs.readFile(path, 'utf8', function(err, data){ if (!err){ callback(data); } else { console.log("fatal error pulling sql file") callback(err); } }); }) server.listen(3000, () => console.log('started'))