Improve use of Node http-server

This commit is contained in:
Joshua Granick
2014-12-30 12:40:03 -08:00
parent 17dce6cb57
commit 954ab3a32c
3 changed files with 33 additions and 13 deletions

View File

@@ -2,9 +2,11 @@
var colors = require('colors'),
httpServer = require('./http-server.js'),
argv = require('optimist').argv,
portfinder = require('portfinder'),
opener = require('opener');
opener = require('opener'),
argv = require('optimist')
.boolean('cors')
.argv;
if (argv.h || argv.help) {
console.log([
@@ -19,8 +21,13 @@ if (argv.h || argv.help) {
" -s --silent Suppress log messages from output",
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
" -o Open browser window after staring the server",
" -c Set cache time (in seconds). e.g. -c10 for 10 seconds.",
" -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.",
" To disable caching, use -c-1.",
"",
" -S --ssl Enable https.",
" -C --cert Path to ssl cert file (default: cert.pem).",
" -K --key Path to ssl key file (default: key.pem).",
"",
" -h --help Print this list and exit."
].join('\n'));
process.exit();
@@ -29,6 +36,7 @@ if (argv.h || argv.help) {
var port = argv.p || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
log = (argv.s || argv.silent) ? (function () {}) : console.log,
ssl = !!argv.S || !!argv.ssl,
requestLogger;
if (!argv.s && !argv.silent) {
@@ -58,19 +66,31 @@ function listen(port) {
};
if (argv.cors) {
options.headers = { 'Access-Control-Allow-Origin': '*' };
options.headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
};
}
if (ssl) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
key: argv.K || argv.key || 'key.pem'
};
}
var server = httpServer.createServer(options);
server.listen(port, host, function() {
var uri = [ssl ? 'https' : 'http', '://', host, ':', port].join('');
log('Starting up http-server, serving '.yellow
+ server.root.cyan
+ ' on port: '.yellow
+ port.toString().cyan);
log('Hit CTRL-C to stop the server');
+ ((ssl) ? ' through'.yellow + ' https'.cyan : '')
+ ' on: '.yellow
+ uri.cyan);
log('Hit CTRL-C to stop the server');
if (argv.o) {
opener('http://127.0.0.1:' + port.toString());
opener(uri);
}
});
}
@@ -83,4 +103,4 @@ if (process.platform !== 'win32') {
log('http-server stopped.'.red);
process.exit();
});
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "http-server",
"version": "0.6.1",
"version": "0.7.4",
"author": "Nodejitsu <support@nodejitsu.com>",
"description": "a simple zero-configuration command-line http server",
"main": "./lib/http-server",

View File

@@ -124,14 +124,14 @@ class HTML5Helper {
LogHelper.info ("", " - \x1b[1mStarting local web server:\x1b[0m http://localhost:" + port);
Thread.create (function () {
/*Thread.create (function () {
Sys.sleep (0.5);
ProcessHelper.openURL ("http://localhost:" + port);
});
});*/
var args = [ server, path, "-p", Std.string (port), "-c-1" ];
var args = [ server, path, "-p", Std.string (port), "-c-1", "-o", "-a", "localhost", "--cors" ];
if (!LogHelper.verbose) {