Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. const express = require('express')
  2. const app = express()
  3. const morgan = require('morgan')
  4. const mysql = require('mysql')
  5.  
  6. app.use(morgan('combined'))
  7.  
  8. var connection = mysql.createConnection({
  9. host : 'localhost',
  10. database : 'dbRestApi',
  11. user : 'root',
  12. password : '1234',
  13. });
  14.  
  15. connection.connect(function(err) {
  16. if (err) {
  17. console.error('Error connecting: ' + err.stack);
  18. return;
  19. }
  20.  
  21. console.log('Connected as id ' + connection.threadId);
  22. });
  23.  
  24. connection.query('SELECT * FROM users', function (error, results, fields) {
  25. if (error)
  26. throw error;
  27.  
  28. results.forEach(result => {
  29. console.log(result);
  30. });
  31. });
  32.  
  33. connection.end();
  34.  
  35.  
  36. MacBook-Pro-xxx:nodejs_restapi xxx$ node app.js
  37. Connected as id 79
  38. RowDataPacket { id: 1, first_name: 'Tymek', last_name: 'Sala' }
  39. RowDataPacket { id: 2, first_name: 'Kuba', last_name: 'Cichy' }
  40. RowDataPacket { id: 3, first_name: 'Huber', last_name: 'Slow' }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement