Using the same function for different Express routes

advertisements

Right now I have a page that opens up and reads an excel sheet and does stuff with that data based on the index.jade file I have. I want to be able to re-use the exact same code for other router pages and only have the excel sheet change for each page so that the data is displayed based on the different excel sheets.

Therefore I would probably just need to turn this code into a function that can be used in each router.get function, and not just the homepage one.

router.get('/homepage', function(req, res) {

 var workbook= xlsx.readFile('C:\Users\user\Desktop\homepage.xlsx');

  var listStuff = [];
  results1 = [];
  results2 = [];
  results3 = [];

 _.each(workbook.SheetNames, function(value, key, collection) {

listStuff.push(value);

var address_of_cell = 'O3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_1 = desired_cell.v;

results1.push(percent_1)

var address_of_cell = 'P3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_2 = desired_cell.v;

results2.push(percent_2)

var address_of_cell = 'Q3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_3 = desired_cell.v;

results3.push(percent_3)
})

res.render('index', { title: 'Blah Blah', listStuff: listStuff, workbook: workbook, percentofData: results1, percentofData2: results2,   percentofData3: results3});
});

So I want more pages like the one above that uses the same code, where only the location of the excel sheet will change among the pages. How can I turn the above code into a function that can be re-used for the next few pages?:

router.get('/page2', function(req, res) {
  var workbook= xlsx.readFile('C:\Users\user\Desktop\page1.xlsx');
  res.send('HelloWorld')
})

router.get('/page3', function(req, res) {
  var workbook= xlsx.readFile('C:\Users\user\Desktop\page2.xlsx');
  res.send('HelloWorld')
})

router.get('/page4', function(req, res) {
  var workbook= xlsx.readFile('C:\Users\user\Desktop\page3.xlsx');
  res.send('HelloWorld')
})

etc.... Therefore I need the excel workbook variable to change from homepage.xlsx to page1.xlsx, page2.xlsx, page3.xlsx, etc.. in the function. I want this all to be done in the same index.js file.


How about if you move all that repetitive logic into a function:

function processXLSXFile(filePath) {
  var workbook= xlsx.readFile(filePath);

  var listStuff = [];
  results1 = [];
  results2 = [];
  results3 = [];

 _.each(workbook.SheetNames, function(value, key, collection) {

  listStuff.push(value);

  var address_of_cell = 'O3'
  var worksheet = workbook.Sheets[value];
  var desired_cell = worksheet[address_of_cell];
  var percent_1 = desired_cell.v;

  results1.push(percent_1)

  var address_of_cell = 'P3'
  var worksheet = workbook.Sheets[value];
  var desired_cell = worksheet[address_of_cell];
  var percent_2 = desired_cell.v;

  results2.push(percent_2)

  var address_of_cell = 'Q3'
  var worksheet = workbook.Sheets[value];
  var desired_cell = worksheet[address_of_cell];
  var percent_3 = desired_cell.v;

  results3.push(percent_3)
  })

  return {
    listStuff: listStuff,
    results1: results1,
    results2: results2,
    results3: results3
  };
}

for then you can reuse it in your route handlers:

router.get('/homepage', function(req, res) {
  var result = processXLSXFile('C:\Users\user\Desktop\homepage.xlsx');
  var listStuff = result.listStuff;
  var workbook = result.workbook;
  var results1 = result.results1;
  var results2 = result.results2;
  var results3 = result.results3;

  res.render('index', { title: 'Blah Blah', listStuff: listStuff, workbook: workbook, percentofData: results1, percentofData2: results2,   percentofData3: results3});
});

router.get('/page2', function(req, res) {
  var result = processXLSXFile('C:\Users\user\Desktop\page1.xlsx');
  var listStuff = result.listStuff;
  var workbook = result.workbook;
  var results1 = result.results1;
  var results2 = result.results2;
  var results3 = result.results3;

  res.send('HelloWorld')
})

router.get('/page3', function(req, res) {
  var result = processXLSXFile('C:\Users\user\Desktop\page2.xlsx');
  var listStuff = result.listStuff;
  var workbook = result.workbook;
  var results1 = result.results1;
  var results2 = result.results2;
  var results3 = result.results3;

  res.send('HelloWorld')
})

router.get('/page4', function(req, res) {
  var result = processXLSXFile('C:\Users\user\Desktop\page3.xlsx');
  var listStuff = result.listStuff;
  var workbook = result.workbook;
  var results1 = result.results1;
  var results2 = result.results2;
  var results3 = result.results3;

  res.send('HelloWorld')
})

Hope this helps you with your doubt.