resultsController.js 1.7 KB

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