title: Upload and read excel file in NodeJs
body: <p>I need to upload an excel sheet and read that in loop to save data in database.  I need to do it inside my custom controller method. I am using multer, xlsx for the same.</p>
  
  <p>Code to upload excel:</p>
  
  <pre><code>&lt;form action="/file/upload" method="POST" encType="multipart/form-data"&gt;
      &lt;input type="file" name="file" accept="*.csv" /&gt;
      &lt;br/&gt;
      &lt;br/&gt;
      &lt;input type="submit" value="Submit" /&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>I have to do excel processing in <code>uploadData</code> method in <code>FileController</code>. Below is code in File Controller:</p>
  
  <pre><code>var bodyParser = require('body-parser');
  var multer = require('multer');
  var xlstojson = require("xls-to-json-lc");
  var xlsxtojson = require("xlsx-to-json-lc");
  
  var storage = multer.diskStorage({ //multers disk storage settings
    destination: function (req, file, cb) {
      cb(null, './public/uploads/')
    },
    filename: function (req, file, cb) {
      var datetimestamp = Date.now();
      cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1])
    }
  });
  var upload = multer({ //multer settings
    storage: storage,
    fileFilter: function (req, file, callback) { //file filter
      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');
  
  function uploadData(req, res) {
    var exceltojson;
    try {
      upload(req, res, function (err) {
        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;
        }
        /** 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;
        }
        try {
          exceltojson({
            input: req.file.path,
            output: null, //since we don't need output.json
            lowerCaseHeaders: true
          }, function (err, result) {
            if (err) {
              return res.json({ error_code: 1, err_desc: err, data: null });
            }
            res.json({ error_code: 0, err_desc: null, data: result });
          });
        } catch (e) {
          res.json({ error_code: 1, err_desc: "Corupted excel file" });
        }
      })
    }
    catch (e) {
      console.log(e);
    }
  }
  </code></pre>
  
  <p>Have defined route as below</p>
  
  <pre><code>'POST /file/upload': {
   controller: 'v1/FileController',
    action: 'uploadData'
  },
  </code></pre>
  
  <p>It goes to <code>uploadData</code> method in controller but then exits abruptly. Not sure what I am missing but struggling from last few hours. I checked many links but all suggests to do everything in <code>app.js</code> file. I am newbie in Node and trying to map examples available online to do stuff in controller method. Any pointers will be much appreciated.</p>