frontController.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var path = require('path');
  2. var express = require('express');
  3. var FrontController = function(app) {
  4. 'use strict';
  5. var cacheDuration = 365 * 24 * 60 * 60 * 1000; // One year
  6. var assetsPath = (app.get('env') === 'development') ? '../../../front/src' : '../../../front/build';
  7. // Routes templating
  8. var routes = ['/', '/about', '/result/:runId', '/result/:runId/timeline', '/result/:runId/screenshot', '/result/:runId/rule/:policy', '/queue/:runId'];
  9. routes.forEach(function(route) {
  10. app.get(route, function(req, res) {
  11. res.setHeader('Cache-Control', 'public, max-age=20');
  12. res.render(path.join(__dirname, assetsPath, 'main.html'), {
  13. baseUrl: app.locals.baseUrl
  14. });
  15. });
  16. });
  17. // Views templating
  18. app.get('/views/:viewName', function(req, res) {
  19. res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration);
  20. res.render(path.join(__dirname, assetsPath, 'views/' + req.params.viewName), {
  21. baseUrl: app.locals.baseUrl
  22. });
  23. });
  24. // Static assets
  25. app.use('/css', express.static(path.join(__dirname, assetsPath, 'css'), { maxAge: cacheDuration }));
  26. app.use('/fonts', express.static(path.join(__dirname, assetsPath, 'fonts'), { maxAge: cacheDuration }));
  27. app.use('/img', express.static(path.join(__dirname, assetsPath, 'img'), { maxAge: cacheDuration }));
  28. app.use('/js', express.static(path.join(__dirname, assetsPath, 'js'), { maxAge: cacheDuration }));
  29. app.use('/node_modules', express.static(path.join(__dirname, '../../../node_modules'), { maxAge: cacheDuration }));
  30. };
  31. module.exports = FrontController;