Last active
October 20, 2016 14:53
-
-
Save labra/dcaa6126e11a2ebf0e649fe1664846a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const formidable = require('formidable'), fs = require('fs'), http = require('http') | |
http.createServer((req, resp ) => { | |
switch (req.method) { | |
case 'GET': pideFichero(req,resp); break; | |
case 'POST': procesaFichero(req,resp); break; | |
}}).listen(3000); | |
function procesaFichero(req,resp) { | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function(err, fields, files) { | |
resp.writeHead(200, {'content-type': 'application/json'}); | |
fs.readFile(files.fichero.path,'utf8', (err,datos) => { | |
if (err) throw err; | |
let json = JSON.parse(datos) | |
resp.end(JSON.stringify(json)); | |
})}); | |
return; | |
} | |
function pideFichero(req,resp) { | |
resp.writeHead(200, {'content-type': 'text/html'}); | |
resp.end('<h1>Cargar fichero JSON</h1>' + | |
'<form action="/" enctype="multipart/form-data" method="post">'+ | |
'<input type="file" name="fichero"><br>'+ | |
'<input type="submit" value="Enviar">'+ | |
'</form>' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment