_middleware-render-ejs.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module.exports = function (req, res, next){
  2. var path = require('path');
  3. var fs = require('fs');
  4. var get_files = require('../node_modules/grunt-adiejs-static/lib/get_files').get_files,
  5. get_data = require('../node_modules/grunt-adiejs-static/lib/data').get_data,
  6. get_layout = require('../node_modules/grunt-adiejs-static/lib/layout').get_layout,
  7. render_file = require('../node_modules/grunt-adiejs-static/lib/render').render_file,
  8. write_file = require('../node_modules/grunt-adiejs-static/lib/write_file').write_file,
  9. get_helper_functions = require('../node_modules/grunt-adiejs-static/lib/get_helper_functions').get_helper_functions,
  10. ejs_static = require('../node_modules/grunt-adiejs-static/lib/ejs_static'),
  11. _ = require('../node_modules/grunt-adiejs-static/node_modules/underscore'),
  12. accepts = require('../node_modules/grunt-contrib-connect/node_modules/connect/node_modules/serve-index/node_modules/accepts'),
  13. url = require('url');
  14. var reqPathName = decodeURIComponent(url.parse(req.originalUrl).pathname),
  15. reqFileName = reqPathName.substring(reqPathName.lastIndexOf('/')+1),
  16. localPathName = path.resolve('src/', reqPathName.substring(1)),
  17. renderedFile;
  18. fs.readFile(localPathName, function(err, file) {
  19. if (err) {return next();}
  20. var options = {
  21. path_to_data: 'src/data/config.json',
  22. file_extension: '.html',
  23. underscore: true
  24. },
  25. config_cover = ejs_static.get_files(options),
  26. config_default = {},
  27. htmlfiles; // local html file array
  28. fs.readdir(path.resolve('src/'), function(err, arr) {
  29. if (err) {console.log(err)}
  30. htmlfiles = _.filter(arr, function(item) {
  31. return item.lastIndexOf('.html') !== -1;
  32. });
  33. htmlfiles = _.map(htmlfiles, function(item) {
  34. return item.substring(0, item.lastIndexOf('.html'));
  35. });
  36. // cover config_default by config_cover
  37. _.each(htmlfiles, function(item) {
  38. config_default[item] = {};
  39. config_default[item]['path_to_layout'] = (config_cover[item] && config_cover[item]['path_to_layout']) || 'src/' + item + '.html';
  40. config_default[item]['path_to_data'] = (config_cover[item] && config_cover[item]['path_to_data']) || ["src/data/global.json"];
  41. });
  42. Object.keys(config_default).forEach(function(key) {
  43. if (reqFileName === key + options.file_extension) {
  44. var fileData = ejs_static.get_data(key, config_default);
  45. var layoutData = ejs_static.get_layout(key, config_default, options);
  46. renderedFile = ejs_static.render_file(layoutData, fileData, _.extend({}, _));
  47. }
  48. });
  49. // set the correct media-type, default 'text/plain'
  50. var type = accepts(req).types('text/plain', 'text/html', 'application/json', 'text/css', 'application/javascript', 'application/x-javascript', 'application/x-font-woff');
  51. switch(type){
  52. case 'text/css':
  53. res.setHeader('Content-Type', 'text/css');
  54. break;
  55. case 'application/javascript':
  56. res.setHeader('Content-Type', 'application/javascript');
  57. break;
  58. case 'application/x-javascript':
  59. res.setHeader('Content-Type', 'application/x-javascript');
  60. break;
  61. case 'text/plain':
  62. req.url.lastIndexOf('.js')!==-1 && res.setHeader('Content-Type', 'application/javascript');
  63. req.url.lastIndexOf('.css')!==-1 && res.setHeader('Content-Type', 'text/css');
  64. req.url.lastIndexOf('.woff')!==-1 && res.setHeader('Content-Type', 'application/x-font-woff');
  65. req.url.lastIndexOf('.svg')!==-1 && res.setHeader('Content-Type', 'application/x-font-svg');
  66. break;
  67. }
  68. res.end(renderedFile || file);
  69. });
  70. });
  71. };