simplehttpserver.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. port = process.argv[2] ? parseInt(process.argv[2], 0) : port;
  21. function handleRequest(request, response) {
  22. var urlObject = urlParser.parse(request.url, true);
  23. var pathname = decodeURIComponent(urlObject.pathname);
  24. console.log('[' + (new Date()).toUTCString() + '] ' + '"' + request.method + ' ' + pathname + '"');
  25. var filePath = path.join(currentDir, pathname);
  26. fs.stat(filePath, function(err, stats) {
  27. if (err) {
  28. response.writeHead(404, {});
  29. response.end('File not found!');
  30. return;
  31. }
  32. if (stats.isFile()) {
  33. fs.readFile(filePath, function(err, data) {
  34. if (err) {
  35. response.writeHead(404, {});
  36. response.end('Opps. Resource not found');
  37. return;
  38. }
  39. var fileType = filePath.split('.').pop().toLowerCase();
  40. response.writeHead(200, {
  41. "Content-Type": mimeTypes[fileType] || mimeTypes['bin']
  42. } );
  43. response.write(data);
  44. response.end();
  45. });
  46. } else if (stats.isDirectory()) {
  47. fs.readdir(filePath, function(error, files) {
  48. if (error) {
  49. response.writeHead(500, {});
  50. response.end();
  51. return;
  52. }
  53. var l = pathname.length;
  54. if (pathname.substring(l-1)!='/') pathname += '/';
  55. response.writeHead(200, {'Content-Type': 'text/html'});
  56. response.write('<!DOCTYPE html>\n<html><head><meta charset="UTF-8"><title>' + filePath + '</title></head><body>');
  57. response.write('<h1>' + filePath + '</h1>');
  58. response.write('<ul style="list-style:none;font-family:courier new;">');
  59. files.unshift('.', '..');
  60. files.forEach(function(item) {
  61. var urlpath = pathname + item,
  62. itemStats = fs.statSync(currentDir + urlpath);
  63. if (itemStats.isDirectory()) {
  64. urlpath += '/';
  65. item += '/';
  66. }
  67. response.write('<li><a href="'+ urlpath + '">' + item + '</a></li>');
  68. });
  69. response.end('</ul></body></html>');
  70. });
  71. }
  72. });
  73. }
  74. http.createServer(handleRequest).listen(port);
  75. require('dns').lookup(require('os').hostname(), function (err, addr, fam) {
  76. console.log('Running at http://' + addr + ((port === 80) ? '' : ':') + port + '/');
  77. });
  78. console.log('Three.js server has started...');
  79. console.log('Base directory at ' + currentDir);