Nicole Pinto
Nicole Pinto

Reputation: 364

Using the same function for various Express routes

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.

Upvotes: 0

Views: 73

Answers (2)

Wilson
Wilson

Reputation: 9136

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.

Upvotes: 1

asa
asa

Reputation: 4040

If you can allow urls like '/page/2', '/page/3' etc... I would recommend doing:

router.get('/page/:id', function(req, res) {
  // do whatever validation test you need with req.params.id

  // then 
  var workbook= xlsx.readFile('C:\Users\user\Desktop\page' + req.params.id + '.xlsx');
  res.send('HelloWorld')
})

Upvotes: 0

Related Questions