Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Loading HTTP module
- var http = require('http');
- var url = require('url');
- //Creating constant relating to DB mysql
- const mysql = require('mysql');
- //Creating db object
- const db = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: 'jaime',
- database: 'pbe'
- });
- /*db.connect(function(err) {
- if (err) throw err;
- console.log("Connected!");
- });
- */
- //db.connect(); <--- CONNECT VIEJO !!!
- const port = 8080;
- // Configurar una respuesta HTTP para todas las peticiones
- //Setting up HTTP responde to all request
- /*function onRequest(request, response) {
- console.log("Peticion Recibida.");
- // response.writeHead(200, {"Content-Type": "text/html"});
- //response.write("Hola Mundo");
- //response.end();
- console.dir(request.param);
- if (request.method == 'POST') {
- console.log('POST');
- var body = '';
- request.on('data', function(data) {
- body += data;
- console.log('Partial body: ' + body);
- })
- request.on('end', function() {
- console.log('Body: ' + body);
- response.writeHead(200, {'Content-Type': 'text/html'});
- response.end('post received');
- })
- } else {
- console.log('GET')
- var html = `
- <html>
- <body>
- <form method="post" action="http://localhost:3000">Name:
- <input type="text" name="name" />
- <input type="submit" value="Submit" />
- </form>
- </body>
- </html>`;
- response.writeHead(200, {'Content-Type': 'text/html'});
- response.end(html);
- }
- }*/
- function find_student(uid){
- //db.connect(function(err) {
- //if (err) throw err;
- //var sentence = ;
- //console.log(sentence);
- db.query("SELECT idstudents FROM students WHERE uid = '" + uid + "'", function (err, result) {
- // if (err) throw err;
- //console.log(result);
- var json = JSON.stringify(result);
- var idstudent = json.substring(json.indexOf(":") + 2 , json.length - 3 );
- console.log(idstudent);
- return idstudent;
- });
- //});
- //db.end();
- }
- var server = http.createServer(function(request, response){
- var q = url.parse(request.url,true).query;
- var a = JSON.stringify(q.uid);
- console.log(typeof(a));
- //response.write('<h1>jodete cabron</h1>');
- response.end(JSON.stringify(find_student("c3868024")));
- //response.end();
- }
- );
- // Escuchar al puerto 8080
- //Listening 8080 port
- server.listen(port);
- // Poner un mensaje en la consola
- //Printing msg in console
- //console.log("Servidor funcionando en http://localhost:8080/");
- //console.log(`Listening at http://${host}:${port}`);
- /*function show_students(){
- db.connect(function(err) {
- if (err) throw err;
- db.query("SELECT * FROM students", function (err, result, fields) {
- if (err) throw err;
- //console.log(result);
- var json = JSON.stringify(result);
- console.log(json);
- // console.log(typeof(objeto_json));
- });
- });
- //db.end();
- }
- */
- //show_students();
- //find_student("c3868024");
Add Comment
Please, Sign In to add comment