Advertisement
Guest User

Untitled

a guest
May 17th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. var http = require('http');
  2. var PORT = 4000;
  3. var mysql = require('mysql');
  4. var con = mysql.createConnection({
  5. host: "localhost",
  6. user: "root",
  7. password: "",
  8. database: "EmployeesDB"
  9. });
  10. con.connect(function (err) {
  11. if (err) throw err;
  12. console.log("Connected!");
  13. });
  14.  
  15. http.createServer( function(req, res ) {
  16. if(req.method == 'GET'){
  17. console.log(req.url);
  18. if(req.url.startsWith('/employees?')) {
  19. /*if indexOf('&') onda split('&')*/
  20. var value=req.url.split('?')[1].split('&'),
  21. limit=100,
  22. offset=1;
  23. if(value[0].startsWith('limit')){
  24. limit=parseInt(value[0].split('=')[1]);
  25. }
  26. if(value[0].startsWith('offset')){
  27. offset=parseInt(value[0].split('=')[1]);
  28. }
  29. if(value[1].startsWith('limit')){
  30. limit=parseInt(value[1].split('=')[1]);
  31. }
  32. if(value[1].startsWith('offset')){
  33. offset=parseInt(value[1].split('=')[1]);
  34. }
  35. console.log(value);
  36. console.log(offset);
  37. var sql = "SELECT first_name,emp_no FROM employees LIMIT ? OFFSET ?";
  38. con.query(sql, [limit,offset],function (err, result) {
  39. if (err) throw err;
  40. console.log(result);
  41. var r = "";
  42. for(var i=0;i<result.length;i++){
  43. r+= result[i].emp_no+result[i].first_name+'\n';
  44. }
  45. res.write(r);
  46. res.end();
  47. });
  48. }
  49. }
  50. } ).listen(PORT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement