Tuesday 9 May 2017

How to execute NodeJS function on page refresh

I wrote a small NodeJS application from an example. Heres the part of my code.

server.js:

var app = express();
var helper = require('./helpers.something.js');

helper.calculate(function(result) {
  app.set('something', result);
});

var router = express.Router();
var home = require(./controllers/home.js);

router.get('/', home.index);

home.js

exports.index = (function(req, res) {
  res.locals.calculations = req.app.get('something');
  res.render('home', {
    data: req.app.get('something');
  });
};

The problem I am trying to resolve now is that helper.calculate function is called only when server is started instead of being called every time the page is refreshed. Could anyone advice me how can I call helper.calculate every time the page is refreshed if I want to use the result in both home.js and server.js files as I'm quite lost in express.js documentation.



via JulioBordeaux

No comments:

Post a Comment