Last active
October 11, 2015 04:36
-
-
Save huttj/bf7563ad398286e503ce to your computer and use it in GitHub Desktop.
Quick and dirty CSV upload and parse to JSON
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<form action="/upload" method="post" enctype="multipart/form-data"> | |
<input type="file" name="upload-file"> | |
<input type="submit" value="Upload"> | |
</form> | |
</body> | |
</html> |
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
var http = require("http"); | |
var Url = require("url"); | |
var sys = require("sys"); | |
var fs = require("fs"); | |
var indexHtml; | |
var server = http.createServer(function(req, res) { | |
// Simple path-based request dispatcher | |
var url = Url.parse(req.url); | |
log(url); | |
switch (url.pathname) { | |
case '/': | |
log('/'); | |
index(req, res); | |
break; | |
case '/upload': | |
log('/upload'); | |
uploadFile(req, res); | |
break; | |
default: | |
log('404'); | |
show404(req, res); | |
break; | |
} | |
}); | |
server.listen(3000, function () { | |
log('running on 3000'); | |
}); | |
function index(req, res) { | |
if (indexHtml) { | |
res.writeHead(200); | |
return res.end(indexHtml); | |
} | |
fs.readFile(__dirname + '/index.html', function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
indexHtml = data.toString(); | |
res.writeHead(200); | |
res.end(indexHtml); | |
}); | |
} | |
function uploadFile(req, res) { | |
var fileData = ''; | |
req.addListener("data", function(chunk) { | |
fileData += chunk; | |
}); | |
req.addListener("end", function() { | |
log('formData', fileData); | |
if (!fileData.match(/\.csv"/)) { | |
log('not a csv?'); | |
return res.end('please upload a CSV'); | |
} | |
var csv = extractFormData(fileData); | |
var json = csvToJSON(csv); | |
console.log(json); | |
res.end(JSON.stringify(json)); | |
}); | |
} | |
function extractFormData(str) { | |
str = str.split(/Content-Type:[^\n]+[\r\n\s]+/)[1]; | |
return str.split(/[\r\n]+----/)[0]; | |
} | |
function csvToJSON(csv) { | |
csv = csv.split(/\r?\n/); | |
var keys = csv[0].split(','); | |
var list = csv.slice(1).map(function (n) { | |
var props = n.split(','); | |
var obj = {}; | |
for (var i = 0; i < keys.length; i++) { | |
obj[keys[i]] = props[i]; | |
} | |
return obj; | |
}); | |
return list; | |
} | |
function show404(req, res) { | |
res.writeHead(404); | |
res.write("You're doing it wrong!"); | |
res.end(); | |
} | |
function log() { | |
console.log.apply(console, arguments); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment