simplehttpserver.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * a barebones HTTP server in JS
  3. * to serve three.js easily
  4. *
  5. * @author zz85 https://github.com/zz85
  6. *
  7. * Usage: node simplehttpserver.js <port number>
  8. *
  9. * do not use in production servers
  10. * and try
  11. * npm install http-server -g
  12. * instead.
  13. */
  14. var port = 8000,
  15. http = require( 'http' ),
  16. urlParser = require( 'url' ),
  17. fs = require( 'fs' ),
  18. path = require( 'path' ),
  19. currentDir = process.cwd(),
  20. mimeTypes = {
  21. "html": "text/html",
  22. "js": "text/javascript",
  23. "css": "text/css",
  24. "jpg": "image/jpeg",
  25. "png": "image/png",
  26. "gif": "image/gif",
  27. "ogg": "audio/ogg",
  28. "mp3": "audio/mpeg",
  29. "mp4": "video/mp4",
  30. "txt": "text/plain",
  31. "bin": "application/octet-stream"
  32. };
  33. port = process.argv[ 2 ] ? parseInt( process.argv[ 2 ], 0 ) : port;
  34. function handleRequest( request, response ) {
  35. var urlObject = urlParser.parse( request.url, true );
  36. var pathname = decodeURIComponent( urlObject.pathname );
  37. console.log( '[' + ( new Date() ).toUTCString() + '] ' + '"' + request.method + ' ' + pathname + '"' );
  38. var filePath = path.join( currentDir, pathname );
  39. fs.stat( filePath, function ( err, stats ) {
  40. if ( err ) {
  41. response.writeHead( 404, {} );
  42. response.end( 'File not found!' );
  43. return;
  44. }
  45. if ( stats.isFile() ) {
  46. fs.readFile( filePath, function ( err, data ) {
  47. if ( err ) {
  48. response.writeHead( 404, {} );
  49. response.end( 'Opps. Resource not found' );
  50. return;
  51. }
  52. var fileType = filePath.split( '.' ).pop().toLowerCase();
  53. response.writeHead( 200, {
  54. "Content-Type": mimeTypes[ fileType ] || mimeTypes[ 'bin' ]
  55. } );
  56. response.write( data );
  57. response.end();
  58. } );
  59. } else if ( stats.isDirectory() ) {
  60. fs.readdir( filePath, function ( error, files ) {
  61. if ( error ) {
  62. response.writeHead( 500, {} );
  63. response.end();
  64. return;
  65. }
  66. var l = pathname.length;
  67. if ( pathname.substring( l - 1 ) != '/' ) pathname += '/';
  68. response.writeHead( 200, { 'Content-Type': 'text/html' } );
  69. response.write( '<!DOCTYPE html>\n<html><head><meta charset="UTF-8"><title>' + filePath + '</title></head><body>' );
  70. response.write( '<h1>' + filePath + '</h1>' );
  71. response.write( '<ul style="list-style:none;font-family:courier new;">' );
  72. files.unshift( '.', '..' );
  73. files.forEach( function ( item ) {
  74. var urlpath = pathname + item,
  75. itemStats = fs.statSync( currentDir + urlpath );
  76. if ( itemStats.isDirectory() ) {
  77. urlpath += '/';
  78. item += '/';
  79. }
  80. response.write( '<li><a href="' + urlpath + '">' + item + '</a></li>' );
  81. } );
  82. response.end( '</ul></body></html>' );
  83. } );
  84. }
  85. } );
  86. }
  87. http.createServer( handleRequest ).listen( port );
  88. require( 'dns' ).lookup( require( 'os' ).hostname(), function ( err, addr, fam ) {
  89. console.log( 'Running at http://' + addr + ( ( port === 80 ) ? '' : ':' ) + port + '/' );
  90. } );
  91. console.log( 'Three.js server has started...' );
  92. console.log( 'Base directory at ' + currentDir );