resultsController.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // Escape "</script>" because it can interfer with the HTML parser
  28. var phantomasResults = results.phantomasResults;
  29. phantomasResults = phantomasResults.replace('</script>', '\\u003c/script>');
  30. var html = results.htmlTemplate;
  31. html = html.replace('%%METADATA%%', results.phantomasMetadata);
  32. html = html.replace('%%RESULTS%%', phantomasResults);
  33. html = html.replace('%%GA_ID%%', googleAnalyticsId);
  34. res.setHeader('Content-Type', 'text/html');
  35. res.send(html);
  36. });
  37. };
  38. module.exports = resultsController;