Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. 'use strict';
  4.  
  5. var colors = require('colors/safe'),
  6. os = require('os'),
  7. httpServer = require('../lib/http-server'),
  8. portfinder = require('portfinder'),
  9. opener = require('opener'),
  10. argv = require('optimist')
  11. .boolean('cors')
  12. .argv;
  13.  
  14. var ifaces = os.networkInterfaces();
  15.  
  16. if (argv.h || argv.help) {
  17. console.log([
  18. 'usage: http-server [path] [options]',
  19. '',
  20. 'options:',
  21. ' -p Port to use [8080]',
  22. ' -a Address to use [0.0.0.0]',
  23. ' -d Show directory listings [true]',
  24. ' -i Display autoIndex [true]',
  25. ' -g --gzip Serve gzip files when possible [false]',
  26. ' -e --ext Default file extension if none supplied [none]',
  27. ' -s --silent Suppress log messages from output',
  28. ' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
  29. ' Optionally provide CORS headers list separated by commas',
  30. ' -o [path] Open browser window after starting the server',
  31. ' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
  32. ' To disable caching, use -c-1.',
  33. ' -U --utc Use UTC time format in log messages.',
  34. '',
  35. ' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
  36. '',
  37. ' -S --ssl Enable https.',
  38. ' -C --cert Path to ssl cert file (default: cert.pem).',
  39. ' -K --key Path to ssl key file (default: key.pem).',
  40. '',
  41. ' -r --robots Respond to /robots.txt [User-agent: *\nDisallow: /]',
  42. ' --no-dotfiles Do not show dotfiles',
  43. ' -h --help Print this list and exit.'
  44. ].join('n'));
  45. process.exit();
  46. }
  47.  
  48. var port = argv.p || parseInt(process.env.PORT, 10),
  49. host = argv.a || '0.0.0.0',
  50. ssl = !!argv.S || !!argv.ssl,
  51. proxy = argv.P || argv.proxy,
  52. utc = argv.U || argv.utc,
  53. logger;
  54.  
  55. if (!argv.s && !argv.silent) {
  56. logger = {
  57. info: console.log,
  58. request: function (req, res, error) {
  59. var date = utc ? new Date().toUTCString() : new Date();
  60. if (error) {
  61. logger.info(
  62. '[%s] "%s %s" Error (%s): "%s"',
  63. date, colors.red(req.method), colors.red(req.url),
  64. colors.red(error.status.toString()), colors.red(error.message)
  65. );
  66. }
  67. else {
  68. logger.info(
  69. '[%s] "%s %s" "%s"',
  70. date, colors.cyan(req.method), colors.cyan(req.url),
  71. req.headers['user-agent']
  72. );
  73. }
  74. }
  75. };
  76. }
  77. else if (colors) {
  78. logger = {
  79. info: function () {},
  80. request: function () {}
  81. };
  82. }
  83.  
  84. if (!port) {
  85. portfinder.basePort = 8080;
  86. portfinder.getPort(function (err, port) {
  87. if (err) { throw err; }
  88. listen(port);
  89. });
  90. }
  91. else {
  92. listen(port);
  93. }
  94.  
  95. function listen(port) {
  96. var options = {
  97. root: argv._[0],
  98. cache: argv.c,
  99. showDir: argv.d,
  100. autoIndex: argv.i,
  101. gzip: argv.g || argv.gzip,
  102. robots: argv.r || argv.robots,
  103. ext: argv.e || argv.ext,
  104. logFn: logger.request,
  105. proxy: proxy,
  106. showDotfiles: argv.dotfiles
  107. };
  108.  
  109. if (argv.cors) {
  110. options.cors = true;
  111. if (typeof argv.cors === 'string') {
  112. options.corsHeaders = argv.cors;
  113. }
  114. }
  115.  
  116. if (ssl) {
  117. options.https = {
  118. cert: argv.C || argv.cert || 'cert.pem',
  119. key: argv.K || argv.key || 'key.pem'
  120. };
  121. }
  122.  
  123. var server = httpServer.createServer(options);
  124. server.listen(port, host, function () {
  125. var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
  126. protocol = ssl ? 'https://' : 'http://';
  127.  
  128. logger.info([colors.yellow('Starting up http-server, serving '),
  129. colors.cyan(server.root),
  130. ssl ? (colors.yellow(' through') + colors.cyan(' https')) : '',
  131. colors.yellow('nAvailable on:')
  132. ].join(''));
  133.  
  134. if (argv.a && host !== '0.0.0.0') {
  135. logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
  136. }
  137. else {
  138. Object.keys(ifaces).forEach(function (dev) {
  139. ifaces[dev].forEach(function (details) {
  140. if (details.family === 'IPv4') {
  141. logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
  142. }
  143. });
  144. });
  145. }
  146.  
  147. if (typeof proxy === 'string') {
  148. logger.info('Unhandled requests will be served from: ' + proxy);
  149. }
  150.  
  151. logger.info('Hit CTRL-C to stop the server');
  152. if (argv.o) {
  153. opener(
  154. protocol + canonicalHost + ':' + port,
  155. { command: argv.o !== true ? argv.o : null }
  156. );
  157. }
  158. });
  159. }
  160.  
  161. if (process.platform === 'win32') {
  162. require('readline').createInterface({
  163. input: process.stdin,
  164. output: process.stdout
  165. }).on('SIGINT', function () {
  166. process.emit('SIGINT');
  167. });
  168. }
  169.  
  170. process.on('SIGINT', function () {
  171. logger.info(colors.red('http-server stopped.'));
  172. process.exit();
  173. });
  174.  
  175. process.on('SIGTERM', function () {
  176. logger.info(colors.red('http-server stopped.'));
  177. process.exit();
  178. });
Add Comment
Please, Sign In to add comment