diff --git a/package.json b/package.json index b5fd709..d0a9b6f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "jest", - "start": "node src/index.js" + "start": "nodemon src/index.js" }, "repository": { "type": "git", @@ -25,7 +25,10 @@ }, "homepage": "https://github.com/CUBETIQ/data-migration-nodejs#readme", "dependencies": { + "express": "^4.17.1", "jest": "^26.6.3", + "multer": "^1.4.2", + "nodemon": "^2.0.7", "read-excel-file": "^5.0.0", "xlsx": "^0.16.9" } diff --git a/src/index.js b/src/index.js index 75f6ff8..d7b9377 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +const express = require('express') // const file = require('fs') // file.readFile('./data/people.csv', 'utf-8', (err, data) => { @@ -14,8 +15,34 @@ // }); +// var XLSX = require('xlsx') +// var workbook = XLSX.readFile('./data/Sanagro Product and Catagory list.xlsx'); +// var xlData = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1']); +// var data = xlData.map((row) => ({ id: row.No, name: row['Product name'] })) + +var app = express(); +app.get('/', function (req, res) { + res.sendFile('/upload.html', { root: __dirname }) +}); + +let port = 4000 +app.listen(port, () => { + console.log(`Example app listening at http://localhost:${port}`) +}) + +var multer = require('multer'); +var upload = multer(); var XLSX = require('xlsx') -var workbook = XLSX.readFile('./data/Sanagro Product and Catagory list.xlsx'); -var xlData = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1']); -var data = xlData.map((row) => ({ id: row.No, name: row['Product name'] })) -console.log(data); +app.use(upload.none()); +app.post('/upload', async function (req, res) { + let input = req.body.myFile; + + let wb = XLSX.read(input, {type : 'binary'}); + + let xlData = XLSX.utils.sheet_to_json(wb.Sheets['Sheet1']); + let data = xlData.map((row) => ({ id: row.No, name: row['Product name'] })) + res.send(data) + console.log('dawdaw', data) +}) + + diff --git a/src/upload.html b/src/upload.html new file mode 100644 index 0000000..e985b08 --- /dev/null +++ b/src/upload.html @@ -0,0 +1,34 @@ + + +
+Click on the "Choose File" button to upload a file:
+ + + + +