Created
January 31, 2017 18:29
-
-
Save juliandavidmr/1fa053fc8c8b15ee27c20cdc0e02b125 to your computer and use it in GitHub Desktop.
Servidor básico de NodeJS. Sin ExpressJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mysql = require('mysql'); | |
var connection = mysql.createConnection({ | |
host: 'localhost', | |
user: 'root', | |
password: 'root', | |
database: 'personas', | |
port: 3306 | |
}); | |
connection.connect(function(error){ | |
if(error){ | |
throw error; | |
}else{ | |
console.log('Conexion correcta.'); | |
} | |
}); | |
connection.end(); | |
var personas = [ | |
{dni: 111, nombre: "Julian", edad: 20}, | |
{dni: 222, nombre: "David", edad: 21}, | |
{dni: 333, nombre: "Natalia", edad: 20} | |
]; | |
var http = require('http'); | |
var puerto = 3000; | |
http.createServer(function (req, res) { | |
var indexado = req.url.indexOf('/personas/'); | |
if (req.method === 'GET') { | |
if (req.url === '/personas') { | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(personas)); | |
console.log("Accediendo a el listado de personas"); | |
return; | |
} | |
if(indexado === 0) { | |
var person = personas.filter(function (p) { | |
return (p.dni == req.url.replace('/personas/', '')) | |
})[0]; | |
if(!person) { | |
res.writeHead(404); | |
res.end(); | |
console.log("No se encontro el servicio."); | |
return; | |
} | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(person)); | |
console.log("Accediendo la informacion de una persona"); | |
return; | |
} | |
} else if (req.method === 'POST') { | |
if(indexado === 0) { | |
(function () { | |
var body = ''; | |
req.on('data', function (data) { | |
body += String(data); | |
}); | |
req.on('end', function () { | |
personas.push(JSON.parse(body)); | |
res.writeHead(201); | |
res.end(); | |
}); | |
}()); | |
} | |
} else if (req.method === 'PUT') { | |
if(indexado === 0) { | |
(function () { | |
var body = ''; | |
req.on('data', function (data) { | |
body += String(data); | |
}); | |
req.on('end', function () { | |
body = JSON.parse(body); | |
this.personas = personas.map(function (p) { | |
if (p.dni == req.url.replace('/personas/', '')) { | |
return body; | |
} | |
return p; | |
}); | |
res.writeHead(200); | |
res.end(); | |
}); | |
}()); | |
} | |
} else if (req.method === 'DELETE') { | |
if (indexado === 0) { | |
personas = personas.filter(function (p) { | |
return (p.dni != req.url.replace('/personas/', '')); | |
}); | |
res.writeHead(200); | |
res.end(); | |
return; | |
} | |
} | |
console.log("Intentando acceder a un servicio que no existe"); | |
res.writeHead(404); | |
res.end(); | |
//console.log('Accediendo con solicitud ' + req.method); | |
}).listen(puerto); | |
console.log("Servidor corriendo en http://127.0.0.1:" + puerto); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment