I am working a on restful api that will read excel sheet and return data in the sheet in json format.but while uploading file(.xls) from postman and sending the request to api. the api gets crashed.
Below is my code :
from app.js
var app= express();
var upload = multer({
fileFilter : function(req, file, callback) {
if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length-1]) === -1) {
return callback(new Error('Wrong extension type'));
}
callback(null, true);
}
}).single('file');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var routes = require("./routes/routes.js")(app,upload);
var server = app.listen(3000, function () {
console.log("Listening on port %s...", server.address().port);
});
routes.js :
app.post('/uploadxl', function(req, res) {
console.log("upload file");
var exceltojson = require("xls-to-json-lc");
upload(req,res,function(err){
console.log("e");
if(err){
res.json({error_code:1,err_desc:err});
return;
}
/** Multer gives us file info in req.file object */
if(!req.file){
res.json({error_code:1,err_desc:"No file passed"});
return;
}
//start convert process
/** Check the extension of the incoming file and
* use the appropriate module
*/
if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
exceltojson = xlsxtojson;
} else {
exceltojson = xlstojson;
}
var node_xj = require("xls-to-json");
node_xj({
input: req.file.path, // input xls
output: "output.json", // output json
sheet: "a" // specific sheetname
}, function(err, result) {
if(err) {
console.log("error");
console.error(err);
} else {
console.log("--------result----");
res.json(result);
}
});
});
});
}
I get this error on my windows command prompt :
upload file e You miss a input file
which clearly means it is entering the error function of multer.