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 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. phantomasResults: function(callback) {
  18. fs.readFile(phantomasResultsPath, {encoding: 'utf8'}, callback);
  19. }
  20. }, function(err, results) {
  21. if (err) {
  22. console.log(err);
  23. return res.status(404).send('Sorry, test not found...');
  24. }
  25. // Escape "</script>" because it can interfer with the HTML parser
  26. var phantomasResults = results.phantomasResults;
  27. phantomasResults = phantomasResults.replace(/<\/script>/g, '\\u003c/script>');
  28. var html = results.htmlTemplate;
  29. html = strReplace(html, '%%RESULTS%%', phantomasResults);
  30. html = strReplace(html, '%%GA_ID%%', googleAnalyticsId);
  31. res.setHeader('Content-Type', 'text/html');
  32. res.send(html);
  33. });
  34. };
  35. module.exports = resultsController;