I am reading uploaded json files data but I need to know how can we validate file format or wrong json format data while uploading or after uploading it.
app.post('/upload', function (req, res) {
let sampleFile;
if (Object.keys(req.files).length == 0) {
res.status(400).send('No files were uploaded.');
return;
}
// console.log('req.files >>>', req.files); // eslint-disable-line
sampleFile = req.files.sampleFile;
uploadPath = __dirname + '/uploads/' + sampleFile.name;
sampleFile.mv(uploadPath, function (err) {
if (err) {
return res.status(500).send(err);
}
// console.log('file', uploadPath);
let rawdata = fs.readFileSync(uploadPath);
var student = JSON.parse(rawdata);
emp = student.employee;
//console.log(emp);
res.render('upload.ejs',{emp:emp,uploadPath:uploadPath});
});
});
I wants to validate json data after uploading it and can I identify JSON objects or json array.