javishelby

Untitled

May 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // Loading HTTP module
  2. var http = require('http');
  3. var url = require('url');
  4.  
  5. //Creating constant relating to DB mysql
  6. const mysql = require('mysql');
  7.  
  8. //Creating db object
  9. const db = mysql.createConnection({
  10. host: 'localhost',
  11. user: 'root',
  12. password: 'jaime',
  13. database: 'pbe'
  14. });
  15.  
  16.  
  17. /*db.connect(function(err) {
  18. if (err) throw err;
  19. console.log("Connected!");
  20. });
  21. */
  22.  
  23. //db.connect(); <--- CONNECT VIEJO !!!
  24.  
  25. const port = 8080;
  26. // Configurar una respuesta HTTP para todas las peticiones
  27. //Setting up HTTP responde to all request
  28.  
  29. /*function onRequest(request, response) {
  30. console.log("Peticion Recibida.");
  31. // response.writeHead(200, {"Content-Type": "text/html"});
  32. //response.write("Hola Mundo");
  33. //response.end();
  34.  
  35. console.dir(request.param);
  36.  
  37.  
  38. if (request.method == 'POST') {
  39. console.log('POST');
  40. var body = '';
  41. request.on('data', function(data) {
  42. body += data;
  43. console.log('Partial body: ' + body);
  44. })
  45. request.on('end', function() {
  46. console.log('Body: ' + body);
  47. response.writeHead(200, {'Content-Type': 'text/html'});
  48. response.end('post received');
  49. })
  50. } else {
  51. console.log('GET')
  52. var html = `
  53. <html>
  54. <body>
  55. <form method="post" action="http://localhost:3000">Name:
  56. <input type="text" name="name" />
  57. <input type="submit" value="Submit" />
  58. </form>
  59. </body>
  60. </html>`;
  61. response.writeHead(200, {'Content-Type': 'text/html'});
  62. response.end(html);
  63. }
  64. }*/
  65. function find_student(uid){
  66. //db.connect(function(err) {
  67. //if (err) throw err;
  68. //var sentence = ;
  69. //console.log(sentence);
  70. db.query("SELECT idstudents FROM students WHERE uid = '" + uid + "'", function (err, result) {
  71. // if (err) throw err;
  72. //console.log(result);
  73. var json = JSON.stringify(result);
  74. var idstudent = json.substring(json.indexOf(":") + 2 , json.length - 3 );
  75. console.log(idstudent);
  76. return idstudent;
  77.  
  78.  
  79.  
  80. });
  81.  
  82. //});
  83. //db.end();
  84. }
  85.  
  86.  
  87. var server = http.createServer(function(request, response){
  88.  
  89. var q = url.parse(request.url,true).query;
  90. var a = JSON.stringify(q.uid);
  91. console.log(typeof(a));
  92. //response.write('<h1>jodete cabron</h1>');
  93.  
  94. response.end(JSON.stringify(find_student("c3868024")));
  95. //response.end();
  96. }
  97. );
  98.  
  99. // Escuchar al puerto 8080
  100. //Listening 8080 port
  101. server.listen(port);
  102.  
  103. // Poner un mensaje en la consola
  104. //Printing msg in console
  105. //console.log("Servidor funcionando en http://localhost:8080/");
  106.  
  107. //console.log(`Listening at http://${host}:${port}`);
  108.  
  109.  
  110. /*function show_students(){
  111. db.connect(function(err) {
  112. if (err) throw err;
  113. db.query("SELECT * FROM students", function (err, result, fields) {
  114. if (err) throw err;
  115. //console.log(result);
  116. var json = JSON.stringify(result);
  117. console.log(json);
  118. // console.log(typeof(objeto_json));
  119.  
  120. });
  121. });
  122.  
  123. //db.end();
  124. }
  125. */
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. //show_students();
  133. //find_student("c3868024");
Add Comment
Please, Sign In to add comment