Created
August 3, 2017 19:32
-
-
Save TalhaAwan/2df4e414f07b55bfa832d4fe2df14ff1 to your computer and use it in GitHub Desktop.
Node/express endpoint to upload and process a csv file
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 fs = require('fs'); | |
var express = require('express'); | |
var multer = require('multer'); | |
var csv = require('fast-csv'); | |
var router = express.Router(); | |
var upload = multer({dest: 'tmp/csv/'}); | |
router.post('/upload', upload.single('file'), function (req, res, next) { | |
var fileRows = [], fileHeader; | |
// open uploaded file | |
csv.fromPath(req.file.path) | |
.on("data", function (data) { | |
fileRows.push(data); // push each row | |
}) | |
.on("end", function () { | |
fs.unlinkSync(req.file.path); // remove temp file | |
//process "fileRows" | |
} | |
} |
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
{ | |
"name": "csvupload", | |
"dependencies": { | |
"express": "~4.13.1", | |
"fast-csv": "^1.0.0", | |
"multer": "^1.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing ); on line 22 as well