resultsController.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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) {
  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. res.setHeader('Content-Type', 'text/html');
  31. res.send(html);
  32. });
  33. };
  34. module.exports = resultsController;