Guest User

Untitled

a guest
Sep 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. var sys = require('util');
  2.  
  3. var Client = require('mysql').Client;
  4. var client = new Client();
  5.  
  6. client.user = 'root';
  7. client.password = 'root';
  8. client.host = 'localhost';
  9. client.port = '8889';
  10.  
  11.  
  12.  
  13. ClientConnectionReady = function(client)
  14. {
  15. client.query('USE joomla17stable', function(error, results) {
  16. if(error) {
  17. console.log('ClientConnectionReady Error: ' + error.message);
  18. client.end();
  19. return;
  20. } else {
  21. console.log('Connected to MySQL');
  22. }
  23. });
  24. };
  25.  
  26. ClientConnectionReady(client);
  27.  
  28. list_events = function(req, res)
  29. {
  30. client.query('SELECT * FROM ndpg5_ohanah_events', function(error, results, fields) {
  31. if (error) {
  32. console.log('GetData Error: ' + error.message);
  33. client.end();
  34. return;
  35. }
  36.  
  37. res.json(results);
  38. });
  39. };
  40.  
  41. read_event = function(req, res)
  42. {
  43. client.query('SELECT * FROM ndpg5_ohanah_events WHERE ohanah_event_id = '+req.params.id, function(error, results, fields) {
  44. if (error) {
  45. console.log('GetData Error: ' + error.message);
  46. client.end();
  47. return;
  48. }
  49.  
  50. res.json(results);
  51. });
  52. };
  53.  
  54. delete_event = function(req, res)
  55. {
  56. var event_id = req.params.id;
  57.  
  58.  
  59. if (event_id && event_id != 0) {
  60. client.query('DELETE FROM ndpg5_ohanah_events WHERE ohanah_event_id = '+req.params.id, function(error, results, fields) {
  61. if (error) {
  62. console.log('GetData Error: ' + error.message);
  63. client.end();
  64. return;
  65. }
  66.  
  67. if (results.affectedRows != 0) {
  68. res.send('OK', 200);
  69. } else {
  70. res.send('Event not found', 404);
  71. }
  72.  
  73. });
  74. } else {
  75. res.send('Please provide a valid event id', 400);
  76. }
  77. };
  78.  
  79. var app = require('express').createServer();
  80.  
  81. // List
  82. app.get('/events', list_events);
  83.  
  84. // Create
  85. //app.post('/events', create_event);
  86.  
  87. // Read
  88. app.get('/events/:id?', read_event);
  89.  
  90. // Update
  91. //app.put('/events/:id?', update_event);
  92.  
  93. // Delete
  94. app.del('/events/:id?', delete_event);
  95.  
  96. app.listen(8887);
  97.  
  98.  
  99.  
  100.  
  101.  
  102. // var auth = exports.auth = {
  103. // username: 'admin',
  104. // password: 'password',
  105. // basicAuth: function (request, body, callback) {
  106. // var realm = "Authorization Required",
  107. // authorization = request.headers.authorization;
  108.  
  109. // if (!authorization) {
  110. // return callback(new journey.NotAuthorized("Authorization header is required."));
  111. // }
  112.  
  113. // var parts = authorization.split(" "), // Basic salkd787&u34n=
  114. // scheme = parts[0], // Basic
  115. // credentials = base64.decode(parts[1]).split(":"); // admin:password
  116.  
  117. // if (scheme !== "Basic") {
  118. // return callback(new journey.NotAuthorized("Authorization scheme must be 'Basic'"));
  119. // }
  120. // else if(!credentials[0] && !credentials[1]){
  121. // return callback(new journey.NotAuthorized("Both username and password are required"));
  122. // }
  123. // else if(credentials[0] !== auth.username || credentials[1] !== auth.password) {
  124. // return callback(new journey.NotAuthorized("Invalid username or password"));
  125. // }
  126.  
  127. // // Respond with no error if username and password match
  128. // callback(null);
  129. // }
  130. // };
Add Comment
Please, Sign In to add comment