Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5c44babde7
Fetching contributors…

Cannot retrieve contributors at this time

executable file 55 lines (49 sloc) 1.356 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/usr/bin/env node

var colors = require('colors'),
    httpServer = require('../lib/http-server'),
    argv = require('optimist').argv,
    portfinder = require('portfinder')

if (argv.h || argv.help) {
  console.log([
    "usage: http-server [path] [options]",
    "",
    "options:",
    " -p Port to use [8080]",
    " -a Address to use [0.0.0.0]",
    " -i Display autoIndex [true]",
    " -s --silent Suppress log messages from output",
    " -h --help Print this list and exit.",
  ].join('\n'));
  process.exit();
}

var port = argv.p,
    host = argv.a || '0.0.0.0',
    log = (argv.s || argv.silent) ? (function () {}) : console.log;

if (!argv.p) {
  portfinder.basePort = 8080;
  portfinder.getPort(function (err, port) {
    if (err) throw err;
    listen(port);
  })
} else {
  listen(port);
}

function listen(port) {
  var options = {
    root: argv._[0],
    autoIndex: argv.i,
    cache: argv.c
  };
  var server = httpServer.createServer(options);
  server.listen(port, host, function() {
    log('Starting up http-server, serving '.yellow
      + server.root.cyan
      + ' on port: '.yellow
      + port.toString().cyan);
    log('Hit CTRL-C to stop the server');
  });
}

process.on('SIGINT', function () {
  log('http-server stopped.'.red);
  process.exit();
});
Something went wrong with that request. Please try again.