Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. var mysql = require("mysql");
  2. var app = require('express')();
  3. var http = require('http').Server(app);
  4. var io = require('socket.io')(http);
  5.  
  6. app.get('/', function(req, res){
  7. res.sendfile('testsql.html');
  8. //res.sendfile('/login/');
  9. });
  10. http.listen(3000, function(){
  11. console.log('listening on *:3000');
  12. });
  13. // First you need to create a connection to the db
  14. var con = mysql.createConnection({
  15. host: "stuff",
  16. user: "stuff",
  17. password: "stuff"
  18. });
  19.  
  20. con.connect(function(err){
  21. if(err){
  22. console.log('Error connecting to Db');
  23. return;
  24. }
  25. console.log('Connection established');
  26. });
  27.  
  28. con.end(function(err) {
  29. // The connection is terminated gracefully
  30. // Ensures all previously enqueued queries are still
  31. // before sending a COM_QUIT packet to the MySQL server.
  32. });
  33. var con = mysql.createConnection({
  34. host: "stuff",
  35. user: "stuff",
  36. password: "stuff",
  37. database: "stuff"
  38. });
  39. con.query('SELECT * FROM users',function(err,rows){
  40. if(err) throw err;
  41.  
  42. console.log('Data received from Db:n');
  43. console.log(rows);
  44. });
  45.  
  46. io.on('connection', function (rows) {
  47. socket.brodcast.emit('showrows', rows);
  48. });
  49.  
  50. <html>
  51. <body>
  52. <p>it Works<p>
  53. <div id="display">
  54. </div>
  55. </body>
  56.  
  57. </html>
  58. <script>
  59. var socket = io();
  60.  
  61. socket.on('connection', function(rows){
  62. jQuery("#display").append(rows);
  63. });
  64. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement