Advertisement
Guest User

Untitled

a guest
Sep 4th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. var express = require('express');
  2. var path = require('path');
  3. var app = express();
  4. var bodyParser = require('body-parser');
  5. var mysql = require('mysql');
  6. var favicon = require('express-favicon');
  7.  
  8. app.use(express.static('public'));
  9. app.use(bodyParser.urlencoded({extended: false}));
  10. app.use(favicon(__dirname + '/public/favicon.ico'));
  11.  
  12. var port = Number(process.env.PORT || 5000);
  13. var pool = mysql.createPool({
  14. host:'hostname',
  15. user:'userid',
  16. password:'password',
  17. database:'db',
  18. port: 3306,
  19. connectionLimit: 100,
  20. dateStrings: true,
  21. timezone: 'ct'
  22. });
  23.  
  24. function handle_database(req,res) {
  25. pool.getConnection(function(err,connection){
  26. if (err) {
  27. connection.release();
  28. console.log('Database Connection failed');
  29. return;
  30. } else {
  31. console.log('Connected to database');
  32. }
  33. });
  34. };
  35.  
  36.  
  37. app.get('/', function (req, res) {
  38. res.sendFile(__dirname + '/public/index.html');
  39. });
  40.  
  41. app.get('/b*', function (req, res) {
  42. bid = path.basename(req.path);
  43. res.sendFile(__dirname + '/public/about.html');
  44. });
  45.  
  46. app.post('/postdata', function (req, res) {
  47. handle_database();
  48. pool.query('SELECT location from beacon where balias = ?', bid,function(err, result) {
  49. if(err){
  50. res.sendFile(__dirname + '/public/beaconerror.html');
  51. } else {
  52. try{
  53. var bloc = result[0].location;
  54. var data ={ beaconId: bid,
  55. status: "Activated",
  56. location: bloc
  57. };
  58. pool.query('INSERT INTO checkin SET ?', data, function(err, result) {
  59. if(err){
  60. throw err;
  61. } else {
  62. res.sendFile(__dirname + '/public/logout.html');
  63. }
  64. });
  65. } catch (error) {
  66. res.sendFile(__dirname + '/public/beaconerror.html');
  67. }
  68. }
  69. });
  70. });
  71.  
  72. app.post('/postcheckout', function(req,res){
  73. handle_database();
  74. pool.query('SELECT location from beacon where balias = ?', bid,function(err, result) {
  75. if(err){
  76. throw err;
  77. } else {
  78. data ={ beaconId: bid,
  79. status: "Deactivated",
  80. location: result[0].location
  81. };
  82. pool.query('INSERT INTO checkin SET ?', data, function(err, result) {
  83. if(err){
  84. throw err;
  85. } else {
  86. res.sendFile(__dirname + '/public/thankyou.html');
  87. }
  88. });
  89. }
  90. });
  91. });
  92.  
  93. app.set('view engine', 'ejs');
  94. var obj = {};
  95. app.get('/data', function(req, res){
  96. handle_database();
  97. pool.query('SELECT * FROM checkin', function(err, result) {
  98. if(err){
  99. throw err;
  100. } else {
  101. obj = {checkin: result};
  102. res.render('layout', obj);
  103. }
  104. });
  105. });
  106.  
  107. app.post('/resettable', function(req, res){
  108. handle_database();
  109. pool.query('TRUNCATE table checkin');
  110. pool.query('SELECT * FROM checkin', function(err, result) {
  111. if(err){
  112. throw err;
  113. } else {
  114. obj = {checkin: result};
  115. res.render('layout', obj);
  116. }
  117. });
  118. });
  119.  
  120. app.listen(port, function(err, req, res){
  121. if (err){
  122. console.log("Failed to start app at port:%s", port);
  123. } else {
  124. console.log("Project app running at port:%s", port);
  125. }
  126. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement