anywhere 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env node
  2. var os = require('os');
  3. var fs = require('fs');
  4. var path = require('path');
  5. var http = require('http');
  6. var https = require('https');
  7. var connect = require('connect');
  8. var serveStatic = require('serve-static');
  9. var serveIndex = require('serve-index');
  10. var fallback = require('connect-history-api-fallback');
  11. var debug = require('debug');
  12. debug.enable('anywhere');
  13. var exec = require('child_process').exec;
  14. var spawn = require('child_process').spawn;
  15. var argv = require("minimist")(process.argv.slice(2), {
  16. alias: {
  17. 'silent': 's',
  18. 'port': 'p',
  19. 'hostname': 'h',
  20. 'dir': 'd',
  21. 'log': 'l',
  22. 'fallback': 'f'
  23. },
  24. string: ['port', 'hostname', 'fallback'],
  25. boolean: ['silent', 'log'],
  26. 'default': {
  27. 'port': 8000,
  28. 'dir': process.cwd()
  29. }
  30. });
  31. if (argv.help) {
  32. console.log("Usage:");
  33. console.log(" anywhere --help // print help information");
  34. console.log(" anywhere // 8000 as default port, current folder as root");
  35. console.log(" anywhere 8888 // 8888 as port");
  36. console.log(" anywhere -p 8989 // 8989 as port");
  37. console.log(" anywhere -s // don't open browser");
  38. console.log(" anywhere -h localhost // localhost as hostname");
  39. console.log(" anywhere -d /home // /home as root");
  40. console.log(" anywhere -l // print log");
  41. console.log(" anywhere -f // Enable history fallback");
  42. process.exit(0);
  43. }
  44. var openURL = function (url) {
  45. switch (process.platform) {
  46. case "darwin":
  47. exec('open ' + url);
  48. break;
  49. case "win32":
  50. exec('start ' + url);
  51. break;
  52. default:
  53. spawn('xdg-open', [url]);
  54. // I use `spawn` since `exec` fails on my machine (Linux i386).
  55. // I heard that `exec` has memory limitation of buffer size of 512k.
  56. // http://stackoverflow.com/a/16099450/222893
  57. // But I am not sure if this memory limit causes the failure of `exec`.
  58. // `xdg-open` is specified in freedesktop standard, so it should work on
  59. // Linux, *BSD, solaris, etc.
  60. }
  61. };
  62. /**
  63. * Get ip(v4) address
  64. * @return {String} the ipv4 address or 'localhost'
  65. */
  66. var getIPAddress = function () {
  67. var ifaces = os.networkInterfaces();
  68. var ip = '';
  69. for (var dev in ifaces) {
  70. ifaces[dev].forEach(function (details) {
  71. if (ip === '' && details.family === 'IPv4' && !details.internal) {
  72. ip = details.address;
  73. return;
  74. }
  75. });
  76. }
  77. return ip || "127.0.0.1";
  78. };
  79. var log = debug('anywhere');
  80. var app = connect();
  81. app.use(function (req, res, next) {
  82. res.setHeader("Access-Control-Allow-Origin", "*");
  83. if (argv.log) {
  84. log(req.method + ' ' + req.url);
  85. }
  86. next();
  87. });
  88. if (argv.fallback !== undefined) {
  89. console.log('Enable html5 history mode.');
  90. app.use(fallback({
  91. index: argv.fallback || '/index.html'
  92. }));
  93. }
  94. app.use(serveStatic(argv.dir, { 'index': ['index.html'] }));
  95. app.use(serveIndex(argv.dir, { 'icons': true }));
  96. // anywhere 8888
  97. // anywhere -p 8989
  98. // anywhere 8888 -s // silent
  99. // anywhere -h localhost
  100. // anywhere -d /home
  101. var port = parseInt(argv._[0] || argv.port, 10);
  102. var secure = port + 1;
  103. var hostname = argv.hostname || getIPAddress();
  104. http.createServer(app).listen(port, function () {
  105. // 忽略80端口
  106. port = (port != 80 ? ':' + port : '');
  107. var url = "http://" + hostname + port + '/';
  108. console.log("Running at " + url);
  109. if (!argv.silent) {
  110. openURL(url);
  111. }
  112. });
  113. var options = {
  114. key: fs.readFileSync(path.join(__dirname, '../keys', 'key.pem')),
  115. cert: fs.readFileSync(path.join(__dirname, '../keys', 'cert.pem'))
  116. };
  117. https.createServer(options, app).listen(secure, function () {
  118. secure = (secure != 80 ? ':' + secure : '');
  119. var url = "https://" + hostname + secure + '/';
  120. console.log("Also running at " + url);
  121. });