Guest User

Untitled

a guest
Mar 11th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. var express = require('express');//Express method
  2. var app = express();
  3. var mysql= require('mysql');
  4. app.use(express.static("."));//Static Pages change if you change the location
  5. var con = mysql.createConnection({
  6. host: 'localhost',
  7. user: 'root',
  8. password: 'Wistful1231',
  9. database: 'students'});
  10.  
  11. app.listen(8080,function(){
  12. console.log("Server's Running") // Just to show the server is up
  13. });
  14.  
  15. app.get('/student',function(req,res){
  16. con.query("SELECT * FROM student",function(err,rows,fields){
  17. if(err){
  18. console.log('Error during query processing');
  19. console.log(err);
  20. res.send('Error during query processing');
  21. }
  22. else{
  23. //contruct html to be sent to client
  24. var Output = '<table border="1"><tr>';
  25. //process column headers
  26. var headers = [];
  27. for(i=0; i<fields.length; i++){
  28. headers.push(fields[i].name);
  29. Output += '<th>' + fields[i].name + '</th>';
  30. }
  31. Output += '</tr>';
  32. //process row values
  33. for(i=0; i<rows.length; i++){
  34. Output += '<tr>';
  35. for(j=0; j<headers.length; j++){
  36. Output += '<td>' + rows[i][headers[j]] + '</td>';
  37. }
  38. Output += '</tr>';
  39. }
  40. Output += '</table>'
  41. console.log('Sending student');
  42. res.send(Output);
  43. }
  44. });
  45. });
  46.  
  47. app.get('/grades',function(req,res){
  48. con.query("SELECT * FROM grades",function(err,rows,fields){
  49. if(err){
  50. console.log('Error during query processing');
  51. console.log(err);
  52. res.send('Error during query processing');
  53. }
  54. else{
  55. //contruct html to be sent to client
  56. var Output = '<table border="1"><tr>';
  57. //process column headers
  58. var headers = [];
  59. for(i=0; i<fields.length; i++){
  60. headers.push(fields[i].name);
  61. Output += '<th>' + fields[i].name + '</th>';
  62. }
  63. Output += '</tr>';
  64. //process row values
  65. for(i=0; i<rows.length; i++){
  66. Output += '<tr>';
  67. for(j=0; j<headers.length; j++){
  68. Output += '<td>' + rows[i][headers[j]] + '</td>';
  69. }
  70. Output += '</tr>';
  71. }
  72. Output += '</table>'
  73. console.log('Sending grades');
  74. res.send(Output);
  75. }
  76. });
  77. });
  78.  
  79. app.get('/courses',function(req,res){
  80. con.query("SELECT * FROM courses",function(err,rows,fields){
  81. if(err){
  82. console.log('Error during query processing');
  83. console.log(err);
  84. res.send('Error during query processing');
  85. }
  86. else{
  87. //contruct html to be sent to client
  88. var Output = '<table border="1"><tr>';
  89. //process column headers
  90. var headers = [];
  91. for(i=0; i<fields.length; i++){
  92. headers.push(fields[i].name);
  93. Output += '<th>' + fields[i].name + '</th>';
  94. }
  95. Output += '</tr>';
  96. //process row values
  97. for(i=0; i<rows.length; i++){
  98. Output += '<tr>';
  99. for(j=0; j<headers.length; j++){
  100. Output += '<td>' + rows[i][headers[j]] + '</td>';
  101. }
  102. Output += '</tr>';
  103. }
  104. Output += '</table>'
  105. console.log('Sending courses');
  106. res.send(Output);
  107. }
  108. });
  109. });
  110.  
  111.  
  112. app.get('/main.html', function(req,res){
  113. res.open(main.html); //Opening the html file
  114. });
  115.  
  116. app.get('/student',function(req,res){
  117. con.query("SELECT * FROM student",function(err,rows,fields){
  118. if(err){
  119. console.log('Error during query processing');
  120. console.log(err);
  121. res.send('Error during query processing');
  122. }
  123. else{
  124. // Data to be sent to html side
  125. var Output = '<table border="1"><tr>';
  126. //Adding tables to html
  127. var headers = [];
  128. for(i=0; i<fields.length; i++){
  129. headers.push(fields[i].name);
  130. Output += '<th>' + fields[i].name + '</th>';
  131. }
  132. Output += '</tr>';
  133. //process row values
  134. for(i=0; i<rows.length; i++){
  135. Output += '<tr>';
  136. for(j=0; j<headers.length; j++){
  137. Output += '<td>' + rows[i][headers[j]] + '</td>';
  138. }
  139. Output += '</tr>';
  140. }
  141. Output += '</table>'
  142. console.log('Sending Student table');
  143. res.send(Output);
  144. }
  145. });
  146. });
  147.  
  148. app.get('/grades',function(req,res){
  149. con.query("SELECT * FROM grades",function(err,rows,fields){
  150. if(err){
  151. console.log('Error ');
  152. console.log(err);
  153. res.send('Error');
  154. }
  155. else{
  156. //Html data to be sent to the client side
  157. var Output = '<table border="1"><tr>';
  158. var headers = [];
  159. for(i=0; i<fields.length; i++){
  160. headers.push(fields[i].name);
  161. Output += '<th>' + fields[i].name + '</th>';
  162. }
  163. Output += '</tr>';
  164. for(i=0; i<rows.length; i++){
  165. Output += '<tr>';
  166. for(j=0; j<headers.length; j++){
  167. Output += '<td>' + rows[i][headers[j]] + '</td>';
  168. }
  169. Output += '</tr>';
  170. }
  171. Output += '</table>'
  172. console.log('Sending Grades Table');
  173. res.send(Output);
  174. }
  175. });
  176. });
  177.  
  178. app.get('/transcript', function (req,res){
  179. //getting name and the term from the user input
  180. var name = req.query.n;
  181. var term = req.query.t;
  182. console.log('Processing transcript request for student ID=' + name);
  183. q_str = 'select student.`Student ID`,`First Name`,`Last Name`,Term,courses.`Course ID`,`Course Description`,Grade from student, courses,grades WHERE student.`Student ID` = grades.studentid AND courses.`Course ID` = grades.courseid AND `First Name` = ' + name + ' ' + 'and Term = ' + term + ' ' + ';';
  184.  
  185. con.query(q_str, function(err,rows,fields){
  186. if(err){
  187. console.log('Error during transcript query processing');
  188. console.log(err);
  189. res.send('Error during transcript query processing');
  190. }
  191. else{
  192.  
  193. var Output = '<table border="1"><tr>';
  194.  
  195. var headers = [];
  196. for(i=0; i<fields.length; i++){
  197. headers.push(fields[i].name);
  198. Output += '<th>' + fields[i].name + '</th>';
  199. }
  200. Output += '</tr>';
  201.  
  202. for(i=0; i<rows.length; i++){
  203. Output += '<tr>';
  204. for(j=0; j<headers.length; j++){
  205. Output += '<td>' + rows[i][headers[j]] + '</td>';
  206. }
  207. Output += '</tr>';
  208. }
  209. Output += '</table>'
  210. console.log('Sending transcript');
  211. res.send(Output);
  212. }
  213. });
  214. });
Add Comment
Please, Sign In to add comment