Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1.  
  2.  
  3. // call the packages we need
  4. var express = require('express');
  5. var app = express();
  6. var app2 = express();
  7. var fs = require('fs');
  8. var Promise = require('promise');
  9. var mysql = require("mysql");
  10.  
  11.  
  12. function allowCrossDomain(res) {
  13. res.setHeader('Access-Control-Allow-Origin', '*');
  14. res.setHeader('Access-Control-Allow-Methods', 'GET');
  15. }
  16.  
  17. /*
  18. * Create the database connection
  19. */
  20. var con = mysql.createConnection({
  21. host: "localhost",
  22. user: "root",
  23. password: "иддш",
  24. database: "phones"
  25. });
  26.  
  27. /*
  28. * Connect to the database
  29. */
  30. function connect() {
  31. con.connect(function(err) {
  32. if(err) {
  33. console.log('Error connecting to databse');
  34. return;
  35. }
  36. console.log('Connection successful');
  37. });
  38. }
  39.  
  40. function disconnect() {
  41. con.end(function(err) {
  42. // The connection is terminated gracefully
  43. // Ensures all previously enqueued queries are still
  44. // before sending a COM_QUIT packet to the MySQL server.
  45. });
  46. }
  47.  
  48.  
  49. var port = process.env.PORT || 1234;
  50. var router = express.Router();
  51.  
  52. /*
  53. * Gets an employee by id
  54. */
  55. router.get('/label/:id', function (req, res) {
  56. allowCrossDomain(res);
  57.  
  58. connect();
  59. con.query('SELECT * FROM phoneLabel WHERE phoneLabel.id = ' + req.params.id, function(err, rows) {
  60. if(err) {
  61. res.json(err);
  62. } else {
  63. res.json(rows);
  64.  
  65. }
  66. console.log('Data received from database:\n');
  67. console.log(rows);
  68.  
  69. });
  70. // disconnect();
  71. });
  72.  
  73. router.get('/labels', function (req, res) {
  74. allowCrossDomain(res);
  75.  
  76. connect();
  77. con.query('SELECT * FROM phoneLabel', function(err, rows) {
  78. if(err) {
  79. res.json(err);
  80. } else {
  81. res.json(rows);
  82.  
  83. }
  84. console.log('Data received from database:\n');
  85. console.log(rows);
  86.  
  87. });
  88. });
  89.  
  90. router.get('/label/model', function(req, res) {
  91. allowCrossDomain(res);
  92.  
  93. connect();
  94. con.query('SELECT * FROM phoneLabel INNER JOIN phoneModel ON phoneModel.labelId = phoneLabel.id', function(err, rows) {
  95. if(err) {
  96. res.json(err);
  97. } else {
  98. res.json(rows);
  99.  
  100. }
  101. console.log('Data received from database:\n');
  102. console.log(rows);
  103.  
  104. });
  105. });
  106.  
  107. router.get('/label/filter?', function(req, res) {
  108. allowCrossDomain(res);
  109.  
  110. connect();
  111. con.query('SELECT * FROM phoneDetails INNER JOIN phoneModel ON phoneModel.id = phoneDetails.modelId INNER JOIN phoneLabel ON phoneLabel.id = phoneModel.labelId WHERE phoneDetails.', function(err, rows) {
  112. if(err) {
  113. res.json(err);
  114. } else {
  115. res.json(rows);
  116. }
  117. console.log('Data received from database:\n');
  118. console.log(rows);
  119. });
  120. });
  121.  
  122. router.put('/add', function (req, res) {
  123. allowCrossDomain(res);
  124.  
  125. // connect();
  126. // con.query('SELECT * FROM phoneLabel', function(err, rows) {
  127. // if(err) {
  128. // res.json(err);
  129. // } else {
  130. // res.json(rows);
  131. // }
  132. // console.log('Data received from database:\n');
  133. // console.log(rows);
  134.  
  135. // });
  136. console.log(res);
  137. // disconnect();
  138. });
  139.  
  140. router.delete('/label/:id', function (req, res) {
  141. allowCrossDomain(res);
  142.  
  143. connect();
  144. con.query('DELETE FROM phoneLabel WHERE phoneLabel.id = ' + req.params.id, function(err, rows) {
  145. if(err) {
  146. res.json(err);
  147. } else {
  148. res.json(rows);
  149. }
  150. console.log('Data received from database:\n');
  151. console.log(rows);
  152. });
  153. // disconnect();
  154. });
  155.  
  156.  
  157. /*
  158. router.get('/total/take_home', function (req, res) {
  159. getTotalFrom('takeHome', res);
  160. });*/
  161.  
  162. /*
  163. * Gets total annual income tax
  164. */
  165. router.get('/total/income_tax', function (req, res) {
  166. getTotalFrom('incomeTax', res);
  167. });
  168.  
  169. /*
  170. * Gets national insurance
  171. */
  172. router.get('/total/national_insurance', function (req, res) {
  173. getTotalFrom('nationalInsurance', res);
  174. });
  175.  
  176. app.use('/', router);
  177. app.listen(port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement