Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. // a shortcut for html element (dom = document object model)
  2. function dom(id) {
  3. return document.getElementById(id);
  4. }
  5.  
  6. // when insert button is clicked, try to send data to server
  7. function sendData() {
  8. let errorMessages = getErrorMessages();
  9. if (errorMessages !== "") {
  10. // if there are errors, show them in msg div
  11. dom("msg").innerHTML = errorMessages;
  12. } else {
  13. // if all values are ok
  14. // create an object with values
  15. let txtStudentName = dom("txtStudentName").value;
  16. let txtStudentAge = dom("txtStudentAge").value;
  17. let student = {
  18. txtStudentName: txtStudentName,
  19. txtStudentAge: txtStudentAge
  20. };
  21. // the xhr object will serve for performing HTTP request
  22. let xhr = new XMLHttpRequest();
  23. // the request will be of type POST and will address "/student" route
  24. xhr.open("POST", "/student");
  25. // the request will send json, UTF8 data
  26. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  27. // transmit the object converted to JSON
  28. xhr.send(JSON.stringify(student));
  29. // every time when a change in HTTP request status occures
  30. xhr.onreadystatechange = function () {
  31. // if HTTP request is DONE
  32. if (xhr.readyState === 4) {
  33. // if status is ok
  34. if (xhr.status === 200) {
  35. dom("msg").innerHTML = "Datele au fost salvate cu succes.";
  36. // if status is not ok
  37. // display error message and the response from server
  38. } else {
  39. dom("msg").innerHTML = "Eroare server la salvarea datelor." + xhr.responseText;
  40. }
  41. }
  42. };
  43. }
  44. }
  45.  
  46. function getErrorMessages() {
  47. // this variable will sum up all the error messages
  48. let errorMessages = "";
  49. // perform 'name' validation
  50. if (!isNameValid()) {
  51. errorMessages += "Numele trebuie să aibă lungimea cuprinsă între 3 și 50 caractere.";
  52. }
  53. // perform 'grade' validation
  54. if (!isAgeValid()) {
  55. errorMessages += "Varsta invalida.";
  56. }
  57. return errorMessages;
  58. }
  59.  
  60. function isNameValid() {
  61. let txtStudentName = dom("txtStudentName");
  62. if (txtStudentName.value.length < 3 || txtStudentName.value.length > 50) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. function isAgeValid() {
  68. let txtStudentAge = dom("txtStudentAge");
  69. if (isNaN(txtStudentAge.value) || txtStudentAge.value < 1 || txtStudentAge.value > 100) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. function showStudentPage(){
  75. dom("pgStudent").style.display="block";
  76. dom("pgStudents").style.display="none";
  77. dom("pgLogin").style.display="none";
  78. }
  79. function showStudentsPage(){
  80. dom("pgStudent").style.display="none";
  81. dom("pgStudents").style.display="block";
  82. dom("pgLogin").style.display="none";
  83. }
  84. function showLoginPage(){
  85. dom("pgStudent").style.display="none";
  86. dom("pgStudents").style.display="none";
  87. dom("pgLogin").style.display="block";
  88.  
  89. }
  90. function hideMessagePage(){
  91. dom("pgMessage").style.display="none";
  92. }
  93.  
  94.  
  95. ??????????????????????
  96. let express = require('express');
  97. let path = require('path');
  98. let bodyParser = require('body-parser');
  99. let mysql = require('mysql');
  100.  
  101. let app = express();
  102.  
  103. app.use(express.static(path.join(__dirname, 'public')));
  104.  
  105. app.use(bodyParser.json());
  106. // define rutes
  107. app.post('/student', function (req, res) {
  108. // get data from client request
  109. let name = req.body.txtStudentName;
  110. let age = req.body.txtStudentAge;
  111.  
  112. const CONNECTION_PARAMS = {
  113. host: "localhost",
  114. port: 55555,
  115. user: "root",
  116. password: "sa",
  117. database: "firstdb"
  118. };
  119.  
  120. let con = mysql.createConnection(CONNECTION_PARAMS);
  121. // open the connection
  122. con.connect(function (err) {
  123. // if connection error occurs, the client must be informed
  124. if (err) {
  125. res.status(500).send("Eroare la conectarea la baza de date." + err.message);
  126. } else {
  127. // prepare the insert query
  128. let sql = "INSERT INTO second (name, age) VALUES ('" + name + "','" + age + "');";
  129. // run query
  130. con.query(sql, function (err, result) {
  131. // if query error occurs, the client must be informed
  132. if (err) {
  133. res.status(500).send("Eroare la inserarea datelor." + err.message);
  134. // if we are here => Hooray, no errors!
  135. } else {
  136. res.status(200).send("");
  137. }
  138. });
  139. }
  140. });
  141. });
  142.  
  143. app.get('/student', function (req, res) {
  144. // get data from client request
  145. let txtStudentName = req.body.txtStudentName;
  146. let txtStudentAge = req.body.txtStudentAge;
  147.  
  148. const CONNECTION_PARAMS = {
  149. host: "localhost",
  150. port: 55555,
  151. user: "root",
  152. password: "sa",
  153. database: "firstdb"
  154. };
  155.  
  156. let con = mysql.createConnection(CONNECTION_PARAMS);
  157. // open the connection
  158. con.connect(function (err) {
  159. // if connection error occurs, the client must be informed
  160. if (err) {
  161. res.status(500).send("Eroare la conectarea la baza de date." + err.message);
  162. } else {
  163. // prepare the insert query
  164. let sql = "SELECT * FROM students";
  165. // run query
  166. con.query(sql, function (err, result) {
  167. // if query error occurs, the client must be informed
  168. if (err) {
  169. res.status(500).send("Eroare la inserarea datelor." + err.message);
  170. // if we are here => Hooray, no errors!
  171. } else {
  172. res.status(200).send(result);
  173. }
  174. });
  175. }
  176. });
  177. });
  178.  
  179. // Define the static folder
  180. app.use(express.static(path.join(__dirname, 'public')));
  181.  
  182. // Inform that server is alive
  183. console.log("Server running on 8080.");
  184. // Listen to a port
  185. app.listen(8080);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement