Ever found yourself trying to build a feature for users to upload files, and suddenly you're knee-deep in weird CSV quirks, Excel formats, and complex JSON structures?
I’ve been there too. Here’s a simple guide to help you handle file uploads in Node.js without losing your mind.
Step 1: Accept File Uploads (With Multer)
First, use multer
to accept file uploads in Express:
npm install multer
Basic setup:
const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();
app.post('/upload', upload.single('file'), (req, res) => {
console.log(req.file);
res.send('File uploaded!');
});
Step 2: Parse Different File Types
1. CSV Files
Use csv-parser
:
npm install csv-parser
Code:
const fs = require('fs');
const csv = require('csv-parser');
fs.createReadStream('uploads/file.csv')
.pipe(csv())
.on('data', (row) => {
console.log(row);
});
2.Excel Files
Excel Files
npm install xlsx
Code:
const xlsx = require('xlsx');
const workbook = xlsx.readFile('uploads/file.xlsx');
const sheetName = workbook.SheetNames[0];
const data = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);
console.log(data);
3.JSON Files
Simple:
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('uploads/file.json', 'utf8'));
console.log(data);
Step 3: Handle Common Data Issues
- Normalize date formats (e.g., with dayjs)
- Remove empty rows
- Deduplicate entries
- Validate column headers
Example (normalize phone numbers):
function cleanPhoneNumber(num) {
return num.replace(/\D/g, '');
}
Step 4: Structure Your Code
- Create a separate module for each file type
- Keep upload logic separate from parsing
- Log errors clearly
Final Thoughts
Handling messy files is something you’ll encounter while creating real-world apps. But don’t worry! With the right tools, you can easily work with CSV, Excel, and JSON files without losing your mind.
Got your own tips or tools? Drop them in the comments!
Top comments (0)