Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ This will install `http-server` globally so that it may be run from the command

`-K` or `--key` Path to ssl key file (default: key.pem).

`-f` or `--forward` Forward to proxy server if static file is not found e.g. -f http://localhost:5000/.

`-h` or `--help` Print this list and exit.

`-c` Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds
Expand Down
8 changes: 7 additions & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (argv.h || argv.help) {
" -C --cert Path to ssl cert file (default: cert.pem).",
" -K --key Path to ssl key file (default: key.pem).",
"",
" -f --forward Forward to proxy server if static file is not found",
" -h --help Print this list and exit."
].join('\n'));
process.exit();
Expand All @@ -37,6 +38,7 @@ 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,
proxy = argv.f || argv.forward,
requestLogger;

if (!argv.s && !argv.silent) {
Expand All @@ -62,7 +64,8 @@ function listen(port) {
showDir: argv.d,
autoIndex: argv.i,
ext: argv.e || argv.ext,
logFn: requestLogger
logFn: requestLogger,
proxy: proxy
};

if (argv.cors) {
Expand All @@ -88,6 +91,9 @@ function listen(port) {
+ ' on: '.yellow
+ uri.cyan);

if(proxy){
log('Forwarding non static requests to '+proxy);
}
log('Hit CTRL-C to stop the server');
if (argv.o) {
opener(uri);
Expand Down
39 changes: 33 additions & 6 deletions lib/http-server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
var fs = require('fs'),
util = require('util'),
union = require('union'),
ecstatic = require('ecstatic');
ecstatic = require('ecstatic'),
httpProxy = require('http-proxy');

var HTTPServer = exports.HTTPServer = function (options) {
var proxyServer;
options = options || {};

if (options.proxy) {
proxyServer = httpProxy.createProxyServer({});
proxyServer.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});

res.end('Something went wrong. And we are reporting a custom error message.');

if (options.logFn) {
console.log('failed proxy ' + options.proxy);
}
});
}

if (options.root) {
this.root = options.root;
}
Expand All @@ -28,9 +45,7 @@ var HTTPServer = exports.HTTPServer = function (options) {
this.autoIndex = options.autoIndex !== 'false';

if (options.ext) {
this.ext = options.ext === true
? 'html'
: options.ext;
this.ext = options.ext === true ? 'html' : options.ext;
}

var serverOptions = {
Expand All @@ -47,8 +62,20 @@ var HTTPServer = exports.HTTPServer = function (options) {
cache: this.cache,
showDir: this.showDir,
autoIndex: this.autoIndex,
defaultExt: this.ext
})
defaultExt: this.ext,
handleError: !options.proxy
}),
function (req, res) {
if (options.proxy) {
if (options.logFn) {
console.log('forwarding');
}
proxyServer.web(req, res, { target: options.proxy });
}
else {
res.emit('next');
}
}
]),
headers: this.headers || {}
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"union": "~0.4.3",
"ecstatic": "~0.5.6",
"portfinder": "0.2.x",
"opener": "~1.4.0"
"opener": "~1.4.0",
"http-proxy": "~1.8.1"
},
"devDependencies": {
"vows": "0.7.x",
Expand Down