Improve use of Node http-server
This commit is contained in:
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
var colors = require('colors'),
|
var colors = require('colors'),
|
||||||
httpServer = require('./http-server.js'),
|
httpServer = require('./http-server.js'),
|
||||||
argv = require('optimist').argv,
|
|
||||||
portfinder = require('portfinder'),
|
portfinder = require('portfinder'),
|
||||||
opener = require('opener');
|
opener = require('opener'),
|
||||||
|
argv = require('optimist')
|
||||||
|
.boolean('cors')
|
||||||
|
.argv;
|
||||||
|
|
||||||
if (argv.h || argv.help) {
|
if (argv.h || argv.help) {
|
||||||
console.log([
|
console.log([
|
||||||
@@ -19,8 +21,13 @@ if (argv.h || argv.help) {
|
|||||||
" -s --silent Suppress log messages from output",
|
" -s --silent Suppress log messages from output",
|
||||||
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
|
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
|
||||||
" -o Open browser window after staring the server",
|
" -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.",
|
" 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."
|
" -h --help Print this list and exit."
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
process.exit();
|
process.exit();
|
||||||
@@ -29,6 +36,7 @@ if (argv.h || argv.help) {
|
|||||||
var port = argv.p || parseInt(process.env.PORT, 10),
|
var port = argv.p || parseInt(process.env.PORT, 10),
|
||||||
host = argv.a || '0.0.0.0',
|
host = argv.a || '0.0.0.0',
|
||||||
log = (argv.s || argv.silent) ? (function () {}) : console.log,
|
log = (argv.s || argv.silent) ? (function () {}) : console.log,
|
||||||
|
ssl = !!argv.S || !!argv.ssl,
|
||||||
requestLogger;
|
requestLogger;
|
||||||
|
|
||||||
if (!argv.s && !argv.silent) {
|
if (!argv.s && !argv.silent) {
|
||||||
@@ -58,19 +66,31 @@ function listen(port) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (argv.cors) {
|
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);
|
var server = httpServer.createServer(options);
|
||||||
server.listen(port, host, function() {
|
server.listen(port, host, function() {
|
||||||
|
var uri = [ssl ? 'https' : 'http', '://', host, ':', port].join('');
|
||||||
log('Starting up http-server, serving '.yellow
|
log('Starting up http-server, serving '.yellow
|
||||||
+ server.root.cyan
|
+ server.root.cyan
|
||||||
+ ' on port: '.yellow
|
+ ((ssl) ? ' through'.yellow + ' https'.cyan : '')
|
||||||
+ port.toString().cyan);
|
+ ' on: '.yellow
|
||||||
log('Hit CTRL-C to stop the server');
|
+ uri.cyan);
|
||||||
|
|
||||||
|
log('Hit CTRL-C to stop the server');
|
||||||
if (argv.o) {
|
if (argv.o) {
|
||||||
opener('http://127.0.0.1:' + port.toString());
|
opener(uri);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "http-server",
|
"name": "http-server",
|
||||||
"version": "0.6.1",
|
"version": "0.7.4",
|
||||||
"author": "Nodejitsu <support@nodejitsu.com>",
|
"author": "Nodejitsu <support@nodejitsu.com>",
|
||||||
"description": "a simple zero-configuration command-line http server",
|
"description": "a simple zero-configuration command-line http server",
|
||||||
"main": "./lib/http-server",
|
"main": "./lib/http-server",
|
||||||
|
|||||||
@@ -124,14 +124,14 @@ class HTML5Helper {
|
|||||||
|
|
||||||
LogHelper.info ("", " - \x1b[1mStarting local web server:\x1b[0m http://localhost:" + port);
|
LogHelper.info ("", " - \x1b[1mStarting local web server:\x1b[0m http://localhost:" + port);
|
||||||
|
|
||||||
Thread.create (function () {
|
/*Thread.create (function () {
|
||||||
|
|
||||||
Sys.sleep (0.5);
|
Sys.sleep (0.5);
|
||||||
ProcessHelper.openURL ("http://localhost:" + port);
|
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) {
|
if (!LogHelper.verbose) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user