Created
July 17, 2017 12:20
-
-
Save thaenor/583d7c075ed5af1cd27ba054682e1962 to your computer and use it in GitHub Desktop.
excel uploader/parser
This file contains 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 express = require('express') | |
const app = express() | |
const router = express.Router(); | |
const multer = require('multer') | |
const upload = multer({ dest: 'uploads/' }) | |
const Parse = require('csv-parse'); | |
const fs = require('fs'); | |
app.use(express.static(__dirname + '/')); | |
function parseCSVFile(sourceFilePath, columns, onNewRecord, handleError, done){ | |
var source = fs.createReadStream(sourceFilePath); | |
var linesRead = 0; | |
var parser = Parse({ | |
delimiter: ',', | |
columns:columns | |
}); | |
parser.on("readable", function(){ | |
var record; | |
while (record = parser.read()) { | |
linesRead++; | |
onNewRecord(record); | |
} | |
}); | |
parser.on("error", function(error){ | |
handleError(error) | |
}); | |
parser.on("end", function(){ | |
done(linesRead); | |
}); | |
source.pipe(parser); | |
} | |
//We will call this once Multer's middleware processed the request | |
//and stored file in req.files.fileFormFieldName | |
function parseFile(req, res, next){ | |
var filePath = req.file.path; | |
console.log(filePath); | |
function onNewRecord(record){ | |
console.log(record) | |
} | |
function onError(error){ | |
console.log(error) | |
} | |
function done(linesRead){ | |
res.send(200, linesRead) | |
} | |
var columns = true; | |
parseCSVFile(filePath, columns, onNewRecord, onError, done); | |
} | |
//this is the route handler with two middlewares. | |
//First: Multer middleware to download file. At some point, | |
//this middleware calls next() so process continues on to next middleware | |
//Second: use the file as you need | |
app.post('/upload', upload.single('uploadArea'), parseFile); | |
app.listen(3000, function () { | |
console.log('... localhost:3000 !') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment