Skip to content

Instantly share code, notes, and snippets.

@TalhaAwan
Created August 3, 2017 19:32
Show Gist options
  • Save TalhaAwan/2df4e414f07b55bfa832d4fe2df14ff1 to your computer and use it in GitHub Desktop.
Save TalhaAwan/2df4e414f07b55bfa832d4fe2df14ff1 to your computer and use it in GitHub Desktop.
Node/express endpoint to upload and process a csv file
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"
}
}
{
"name": "csvupload",
"dependencies": {
"express": "~4.13.1",
"fast-csv": "^1.0.0",
"multer": "^1.1.0"
}
}
@mohammadfarooqi
Copy link

missing a ); on line 21

@nikzayn
Copy link

nikzayn commented Apr 1, 2020

Missing ); on line 22 as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment