AitorAlejandro
4/8/2015 - 9:43 PM

Servidor basico de ficheros

Servidor basico de ficheros

var http = require('http');
var fs = require('fs');


function onRequest(request, response){
    if(request.method === 'GET' && request.url == '/'){
        response.writeHead(200, {'Content-Type':'text/html'});
        fs.createReadStream('./index.html').pipe(response);
    }else{
        send404Response(response);
    }
}


function send404Response(response){
    response.writeHead(404, {'Content-Type': 'text/plain'});
    response.write('Error 404: pagina no encontrada');
    response.end();
}

http.createServer(onRequest).listen(8888);
console.log('El servidor esta ahora corriendo en http://localhost:8888');
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>aitor alejandro</title>
</head>
<body>
    NodeJS es la hostia
</body>
</html>