serve.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // serve.js
  2. // This file contains the low level helper functions that the VWF nodeJS server uses to
  3. // actually serve content.
  4. var filecache = require( './file-cache' ),
  5. YAML = require( 'js-yaml' ),
  6. fs = require( 'fs' ),
  7. helpers = require( './helpers' );
  8. // First, if the FileCache has not been instantiated, do so.
  9. if ( global.FileCache == undefined ) {
  10. var FileCache = new filecache._FileCache( );
  11. global.FileCache = FileCache;
  12. }
  13. // Basic helper function to redirect a request.
  14. function ServeRedirect( url, response ) {
  15. response.writeHead( 302, { 'Location': url } );
  16. response.end();
  17. }
  18. //Just serve a simple file, using the FileCache implementation.
  19. function ServeFile( request, filename, response, URL ) {
  20. FileCache.ServeFile( request, filename, response, URL );
  21. }
  22. //Return a 404 file and not found code
  23. function _404 ( response, file404 ) {
  24. var url404 = helpers.JoinPath( global.applicationRoot, file404 );
  25. if ( helpers.IsFile( url404 ) ) {
  26. fs.readFile( url404, function( error, data ) {
  27. response.writeHead( 404, {'content-type': 'text/html'} );
  28. response.end( data );
  29. });
  30. } else {
  31. response.writeHead( 404, {
  32. "Content-Type": "text/plain",
  33. "Access-Control-Allow-Origin": "*"
  34. } );
  35. response.write( "404 Not Found\n" );
  36. response.end();
  37. }
  38. }
  39. // Parse and serve a JSON file
  40. // Note: If we receive a 'callback' query, we need to
  41. // serve the file as application/javascript and
  42. // adjust slightly.
  43. function ServeJSONFile( filename, response, URL ) {
  44. fs.readFile( filename, "utf8", function ( err, data ) {
  45. if ( err ) {
  46. response.writeHead( 500, {
  47. "Content-Type": "text/plain"
  48. } );
  49. response.write( err + "\n" );
  50. response.end();
  51. return;
  52. }
  53. var type = "application/json";
  54. var jsonString = data;
  55. var callback = URL.query.callback;
  56. if ( callback ) {
  57. jsonString = callback + "(" + data + ")";
  58. type = "application/javascript";
  59. }
  60. response.writeHead( 200, {
  61. "Content-Type": type
  62. } );
  63. response.write( jsonString, "utf-8" );
  64. response.end();
  65. } );
  66. }
  67. //Parse and serve a YAML file
  68. function ServeYAML( filename, response, URL ) {
  69. var tf = filename;
  70. fs.readFile( filename, "utf8", function ( err, data ) {
  71. if (err) {
  72. response.writeHead( 500, {
  73. "Content-Type": "text/plain"
  74. } );
  75. response.write( err + "\n" );
  76. response.end();
  77. return;
  78. }
  79. // Remove the Byte Order Mark (BOM) if one exists
  80. var file = data.replace( /^\uFEFF/, '' );
  81. //global.log(tf);
  82. try {
  83. var deYAML = JSON.stringify( YAML.load( file ) );
  84. } catch ( e ) {
  85. global.log( "error parsing YAML " + filename );
  86. _404( response );
  87. return;
  88. }
  89. var type = "application/json";
  90. var callback = URL.query.callback;
  91. if ( callback ) {
  92. deYAML = callback + "(" + deYAML + ")";
  93. type = "application/javascript";
  94. }
  95. response.writeHead( 200, {
  96. "Content-Type": type
  97. } );
  98. response.write( deYAML, "utf8" );
  99. response.end();
  100. } );
  101. }
  102. //Serve a JSON object
  103. function ServeJSON( jsonobject, response, URL ) {
  104. response.writeHead( 200, {
  105. "Content-Type": "application/json"
  106. } );
  107. response.write( JSON.stringify( jsonobject ), "utf8" );
  108. response.end();
  109. }
  110. exports.Redirect = ServeRedirect;
  111. exports.File = ServeFile;
  112. exports._404 = _404;
  113. exports.JSONFile = ServeJSONFile;
  114. exports.YAML = ServeYAML;
  115. exports.JSON = ServeJSON;