Advertisement
Guest User

server.js

a guest
Apr 29th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require("express");
  2. var app = express();
  3. var ejs = require("ejs");
  4. var bodyParser = require("body-parser");
  5. var oracledb = require('oracledb');
  6.  
  7.  
  8. var username = "tasfik3";
  9. var pass = "root";
  10. var binds;
  11. var host = "127.0.0.1";
  12. var port = 9000;
  13. var link = "http://127.0.0.1:9000/";
  14.  
  15. var btn=1;
  16. var log=1;
  17.  
  18.  
  19. app.use(bodyParser.urlencoded({ extended: true }));
  20. //app.use(express.static(__dirname + '/design')); //this add the folder for css file
  21. app.use('/design', express.static('design'));
  22. app.set("view engine", "ejs");//for this line, file type is not needed in render()
  23.  
  24. app.get("/", function (req, res) {
  25.  
  26.     res.render('home', {
  27.         btn:btn,
  28.         link: link
  29.     });
  30. });
  31.  
  32.  
  33. app.get("/r/:test", function (req, res) {
  34.  
  35.     res.render('home', {
  36.         btn:btn,
  37.         link: link
  38.     });
  39. });
  40.  
  41. app.get("/login-viewer", function (req, res) {
  42.  
  43.     log=3;
  44.     btn=1;
  45.     res.render('login', {
  46.         flag:true,
  47.         log:log,
  48.         btn:btn,
  49.         link: link
  50.     });
  51. });
  52. app.get("/login-developer", function (req, res) {
  53.  
  54.     log=3;
  55.     btn=2;
  56.     res.render('login', {
  57.         flag:true,
  58.         log:log,
  59.         btn:btn,
  60.         link: link
  61.     });
  62. });
  63. app.get("/login-admin", function (req, res) {
  64.  
  65.     log=3;
  66.     btn=3;
  67.     res.render('login', {
  68.         flag:true,
  69.         log:log,
  70.         btn:btn,
  71.         link: link
  72.     });
  73. });
  74.  
  75.  
  76. app.get("/get", function (req, res) {
  77.     res.render('get', {
  78.         btn:btn,
  79.         flag:true,
  80.         link: link
  81.     });
  82. });
  83.  
  84. app.get("/query/:table", function (req, res) {
  85.     console.log(req.params.table);
  86.  
  87.     oracledb.getConnection({
  88.             user: username,
  89.             password: pass,
  90.             connectString: "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= xe)))"
  91.         },
  92.         function (err, connection) {
  93.             if (err) {
  94.                 console.error("error in submitted: " + err.message);
  95.                 return;
  96.             }
  97.  
  98.  
  99.             var colQuery = "SELECT column_name FROM all_tab_cols WHERE table_name = :tn order by COLUMN_ID";
  100.  
  101.             var attribute2=[];
  102.             console.log("oracle entered");
  103.  
  104.                 connection.execute(colQuery,[req.params.table], function (err, result) {
  105.                 if (err) {
  106.                     console.log(err);
  107.                     return;
  108.                 } else {
  109.                     console.log("no error in query1");
  110.                 }
  111.  
  112.                 console.log(result.rows);
  113.  
  114.  
  115.                  attribute2 = result.rows;
  116.                 console.log("checking1..............");
  117.                     var sqlQuery = `select * from ${req.params.table}`;
  118.                  console.log(result.rows);
  119.                  connection.execute(sqlQuery, function (err, result) {
  120.                      if (err) {
  121.                          console.log(err);
  122.                          return;
  123.                      } else {
  124.                          console.log("no error in query2");
  125.                      }
  126.                     // console.log(attribute2);
  127.                      console.log(result.rows);
  128.  
  129.  
  130.                      //var attribute = ["name", "ID", "Nationality", "Religion", "Gender", "Email_address", "mobile", "Birth_Date", "city", "Street", "Zip_code", "Height", "Weight"];
  131.  
  132.                      var MX = result.rows.length;
  133.                      var len = attribute2.length;
  134.  
  135.                      res.render('query', {
  136.                          tn:req.params.table,
  137.                          btn:btn,
  138.                          attribute: attribute2,
  139.                          value: result.rows,
  140.                          link: link,
  141.                          MX: MX,
  142.                          len: len
  143.                      });
  144.                  });
  145.  
  146.             });
  147.  
  148.  
  149.  
  150.         }
  151.     );
  152.  
  153.  
  154.  
  155. });
  156.  
  157. app.post("/post", function (req, res) {
  158.     console.log(req.body);
  159.  
  160.     oracledb.getConnection({
  161.             user: username,
  162.             password: pass,
  163.             connectString: "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= xe)))"
  164.         },
  165.         function (err, connection) {
  166.             if (err) {
  167.                 console.error("error in submitted: " + err.message);
  168.                 return;
  169.             }
  170.  
  171.             var sqlQuery = 'INSERT INTO person VALUES (:t1,:t2,:t3,:t4,:t5,:t6,:t7,:t8,:t9,:t10,:t11,:t12)';
  172.              var date= new Date(req.body.Date);
  173.             binds = [{
  174.                 t1: req.body.ID,
  175.                 t2: req.body.Name,
  176.                 t3: date,
  177.                 t4: req.body.Email,
  178.                 t5: req.body.Nationality,
  179.                 t6: req.body.BloodGroup,
  180.                 t7: req.body.Religion,
  181.                 t8: req.body.Gender,
  182.                 t9: req.body.City,
  183.                 t10: req.body.ZipCode,
  184.                 t11: req.body.Street,
  185.                 t12: req.body.Appartment
  186.             }];
  187.             console.log("oracle entered bind=" + binds[0]);
  188.             console.log("binds = " + binds);
  189.             console.log("\n t1=" + binds[0].t1 + "\n t2=" + binds[0].t2 + "\n t3=" + binds[0].t3 + "\n t4=" + binds[0].t4 + "\n t5=" + binds[0].t5 + "\n t6=" + binds[0].t6 + "\n t7=" + binds[0].t7 + "\n t8=" + binds[0].t8 + "\n t9=" + binds[0].t9 + "\n t10=" + binds[0].t10 + "\n t11=" + binds[0].t11 + "\n t12=" + binds[0].t12);
  190.  
  191.             connection.execute(sqlQuery, binds[0], {
  192.                 autoCommit: true
  193.             }, function (err, result) {
  194.                 if (err) {
  195.                     console.log(err);
  196.                     return;
  197.                 } else{
  198.                     console.log('Rows inserted: ' + result.rowsAffected);
  199.                     return;
  200.                 }
  201.             });
  202.         }
  203.     );
  204.  
  205.     res.render('get', {
  206.         btn:btn,
  207.         flag: false,
  208.         link: link
  209.     });
  210. });
  211.  
  212.  
  213. app.get("/register", function (req, res) {
  214.         log=1;
  215.  
  216.     res.render('login', {
  217.         btn:btn,
  218.         log:log,
  219.         flag:true,
  220.         link: link
  221.     });
  222. });
  223.  
  224.  
  225. app.post("/submitted", function (req, res) {
  226.     console.log(req.body);
  227.  
  228.     oracledb.getConnection({
  229.             user: username,
  230.             password: pass,
  231.             connectString: "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= xe)))"
  232.         },
  233.         function (err, connection) {
  234.             if (err) {
  235.                 console.error("error in submitted: " + err.message);
  236.                 return;
  237.             }
  238.  
  239.             binds = [{
  240.                 t1: req.body.name,
  241.                 t2: req.body.email,
  242.                 t3: req.body.password,
  243.                 t4: req.body.role
  244.             }];
  245.             var sqlQuery = 'INSERT INTO users VALUES (:t1,:t2,:t3,:t4)';
  246.  
  247.             connection.execute(sqlQuery, binds[0], {
  248.                 autoCommit: true
  249.             }, function (err, result) {
  250.                 if (err) {
  251.                     console.log(err);
  252.                     return;
  253.                 } else{
  254.                     console.log('Rows inserted: ' + result.rowsAffected);
  255.                     return;
  256.                 }
  257.             });
  258.         }
  259.     );
  260.  
  261.     res.render('login', {
  262.         btn:btn,
  263.         log:log,
  264.         flag: false,
  265.         link: link
  266.     });
  267. });
  268.  
  269.  
  270.  
  271.  
  272.  
  273. app.post("/logged", function (req, res) {
  274.     console.log(req.body);
  275.  
  276.     oracledb.getConnection({
  277.             user: username,
  278.             password: pass,
  279.             connectString: "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SID= xe)))"
  280.         },
  281.         function (err, connection) {
  282.             if (err) {
  283.                 console.error("error in submitted: " + err.message);
  284.                 return;
  285.             }
  286.  
  287.             binds = [{
  288.                 t1: req.body.name,
  289.                 t2: req.body.email,
  290.                 t3: req.body.password,
  291.                 t4: req.body.role
  292.             }];
  293.             var sqlQuery = "select * from users where  name = :n and email= :e and password=:p and role=:r";
  294.  
  295.             connection.execute(sqlQuery,[binds[0].t1,binds[0].t2,binds[0].t3,binds[0].t4], function (err, result) {
  296.             if (err || result.rows.length==0 ||(btn==1 && binds[0].t4!='VIEWER')||(btn==2 && binds[0].t4!='DEVELOPER')||(btn==3 && binds[0].t4!='ADMIN')) {
  297.                 console.log("login failed!!");
  298.                 btn=1;
  299.                 log=3;
  300.                 res.render('home', {
  301.                     btn:btn,
  302.                     log:log,
  303.                     flag: false,
  304.                     link: link
  305.                 });
  306.                 return;
  307.             } else {
  308.                 console.log("login successful!!");
  309.  
  310.                 log=3;
  311.                 res.render('home', {
  312.                     btn:btn,
  313.                     log:log,
  314.                     flag: false,
  315.                     link: link
  316.                 });
  317.             }
  318.  
  319.  
  320.  
  321.  
  322.              attribute2 = result.rows;
  323.             console.log("checking1..............");
  324.              console.log(result.rows.length);
  325.  
  326.         });
  327.         }
  328.     );
  329.  
  330.  
  331.  
  332. });
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. app.get("/*", function (req, res) {
  340.     //gets everything if above get function are not used
  341.     res.send("<h1><center>"+"Error 404 : PAGE NOT FOUND!!"+"</center></h1>");
  342. });
  343.  
  344. app.listen(port, host, function (err) {
  345.  
  346.     if (err)
  347.         console.log("error happend");
  348.  
  349.     console.log("server has started on link : " + link);
  350. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement