resultsController.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * The page that dispays the results
  3. */
  4. var async = require('async');
  5. var fs = require ('fs');
  6. var resultsController = function(req, res, googleAnalyticsId) {
  7. 'use strict';
  8. var testId = req.params.testId;
  9. var resultsPath = 'results/' + testId;
  10. var phantomasResultsPath = resultsPath + '/results.json';
  11. console.log('Opening test ' + testId + ' results as HTML');
  12. async.parallel({
  13. htmlTemplate: function(callback) {
  14. fs.readFile('./app/node_views/results.html', {encoding: 'utf8'}, callback);
  15. },
  16. phantomasMetadata: function(callback) {
  17. fs.readFile('./node_modules/phantomas/lib/metadata/metadata.json', {encoding: 'utf8'}, callback);
  18. },
  19. phantomasResults: function(callback) {
  20. fs.readFile(phantomasResultsPath, {encoding: 'utf8'}, callback);
  21. }
  22. }, function(err, results) {
  23. if (err) {
  24. console.log(err);
  25. return res.status(404).send('Sorry, test not found...');
  26. }
  27. var html = results.htmlTemplate;
  28. html = html.replace('%%METADATA%%', results.phantomasMetadata);
  29. html = html.replace('%%RESULTS%%', results.phantomasResults);
  30. html = html.replace('%%GA_ID%%', googleAnalyticsId);
  31. res.setHeader('Content-Type', 'text/html');
  32. res.send(html);
  33. });
  34. };
  35. module.exports = resultsController;