Skip to content

Commit

Permalink
Merge branch 'master' into proxy-secure-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
johntron committed Jun 26, 2017
2 parents 9210faa + 402ad49 commit 2af9dee
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 61 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/*
!node_modules/node-static
npm-debug.log
npm-debug.log
17 changes: 4 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "4.1"
- "4"
- "5"
- "6"
- "7"

before_install:
- travis_retry npm install -g npm@2.14.5
- travis_retry npm install

script:
- npm test

matrix:
allow_failures:
- node_js: "0.10"

notifications:
email:
- travis@nodejitsu.com
irc: "irc.freenode.org#nodejitsu"
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[![build status](https://img.shields.io/travis/indexzero/http-server.svg?style=flat-square)](https://travis-ci.org/indexzero/http-server)
[![dependencies status](https://img.shields.io/david/indexzero/http-server.svg?style=flat-square)](https://david-dm.org/indexzero/http-server)
[![npm](https://img.shields.io/npm/v/http-server.svg?style=flat-square)](https://www.npmjs.com/package/http-server)
[![license](https://img.shields.io/github/license/indexzero/http-server.svg?style=flat-square)](https://github.com/indexzero/http-server)

# http-server: a command-line http server

`http-server` is a simple, zero-configuration command-line http server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development, and learning.
Expand All @@ -18,28 +23,8 @@ This will install `http-server` globally so that it may be run from the command

`[path]` defaults to `./public` if the folder exists, and `./` otherwise.

# Installing as a node app

mkdir myapp
cd myapp/
jitsu install http-server

*If you do not have `jitsu` installed you can install it via `npm install jitsu -g`*

## Usage

### Starting http-server locally

node bin/http-server

*Now you can visit http://localhost:8080 to view your server*

### Deploy http-server to nodejitsu

jitsu deploy

*You will now be prompted for a `subdomain` to deploy your application on*

## Available Options:

`-p` Port to use (defaults to 8080)
Expand All @@ -50,6 +35,8 @@ This will install `http-server` globally so that it may be run from the command

`-i` Display autoIndex (defaults to 'True')

`-g` or `--gzip` When enabled (defaults to 'False') it will serve `./public/some-file.js.gz` in place of `./public/some-file.js` when a gzipped version of the file exists and the request accepts gzip encoding.

`-e` or `--ext` Default file extension if none supplied (defaults to 'html')

`-s` or `--silent` Suppress log messages from output
Expand All @@ -75,3 +62,17 @@ This will install `http-server` globally so that it may be run from the command
`-r` or `--robots` Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')

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

# Development

Checkout this repository locally, then:

```sh
$ npm i
$ node bin/http-server
```

*Now you can visit http://localhost:8080 to view your server*

You should see the turtle image in the screenshot above hosted at that URL. See
the `./public` folder for demo content.
45 changes: 27 additions & 18 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

var colors = require('colors'),
var colors = require('colors/safe'),
os = require('os'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
Expand All @@ -22,6 +22,7 @@ if (argv.h || argv.help) {
' -a Address to use [0.0.0.0]',
' -d Show directory listings [true]',
' -i Display autoIndex [true]',
' -g --gzip Serve gzip files when possible [false]',
' -e --ext Default file extension if none supplied [none]',
' -s --silent Suppress log messages from output',
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
Expand All @@ -39,6 +40,7 @@ if (argv.h || argv.help) {
' -K --key Path to ssl key file (default: key.pem).',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
' -h --help Print this list and exit.'
].join('\n'));
process.exit();
Expand All @@ -59,14 +61,14 @@ if (!argv.s && !argv.silent) {
if (error) {
logger.info(
'[%s] "%s %s" Error (%s): "%s"',
date, req.method.red, req.url.red,
error.status.toString().red, error.message.red
date, colors.red(req.method), colors.red(req.url),
colors.red(error.status.toString()), colors.red(error.message)
);
}
else {
logger.info(
'[%s] "%s %s" "%s"',
date, req.method.cyan, req.url.cyan,
date, colors.cyan(req.method), colors.cyan(req.url),
req.headers['user-agent']
);
}
Expand Down Expand Up @@ -97,9 +99,11 @@ function listen(port) {
cache: argv.c,
showDir: argv.d,
autoIndex: argv.i,
gzip: argv.g || argv.gzip,
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: logger.request
logFn: logger.request,
showDotfiles: argv.dotfiles
};

if (proxy) {
Expand Down Expand Up @@ -129,19 +133,24 @@ function listen(port) {
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
protocol = ssl ? 'https://' : 'http://';

logger.info(['Starting up http-server, serving '.yellow,
server.root.cyan,
ssl ? (' through'.yellow + ' https'.cyan) : '',
'\nAvailable on:'.yellow
logger.info([colors.yellow('Starting up http-server, serving '),
colors.cyan(server.root),
ssl ? (colors.yellow(' through') + colors.cyan(' https')) : '',
colors.yellow('\nAvailable on:')
].join(''));

Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + port.toString()).green);
}
if (argv.a && host !== '0.0.0.0') {
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
}
else {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
}
});
});
});
}

if (typeof proxy === 'string') {
logger.info('Unhandled requests will be served from: ' + proxy);
Expand All @@ -150,7 +159,7 @@ function listen(port) {
logger.info('Hit CTRL-C to stop the server');
if (argv.o) {
opener(
protocol + '//' + canonicalHost + ':' + port,
protocol + canonicalHost + ':' + port,
{ command: argv.o !== true ? argv.o : null }
);
}
Expand All @@ -167,11 +176,11 @@ if (process.platform === 'win32') {
}

process.on('SIGINT', function () {
logger.info('http-server stopped.'.red);
logger.info(colors.red('http-server stopped.'));
process.exit();
});

process.on('SIGTERM', function () {
logger.info('http-server stopped.'.red);
logger.info(colors.red('http-server stopped.'));
process.exit();
});
4 changes: 4 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function HttpServer(options) {
this.cache = options.cache === undefined ? 3600 : options.cache; // in seconds.
this.showDir = options.showDir !== 'false';
this.autoIndex = options.autoIndex !== 'false';
this.showDotfiles = options.showDotfiles;
this.gzip = options.gzip === true;
this.contentType = options.contentType || 'application/octet-stream';

if (options.ext) {
Expand Down Expand Up @@ -95,8 +97,10 @@ function HttpServer(options) {
root: this.root,
cache: this.cache,
showDir: this.showDir,
showDotfiles: this.showDotfiles,
autoIndex: this.autoIndex,
defaultExt: this.ext,
gzip: this.gzip,
contentType: this.contentType,
handleError: typeof options.proxy !== 'object'
}));
Expand Down
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-server",
"version": "0.8.5",
"version": "0.10.0",
"description": "A simple zero-configuration command-line http server",
"main": "./lib/http-server",
"repository": {
Expand All @@ -16,6 +16,10 @@
"pretest": "common bin/http-server lib/ test",
"test": "vows --spec --isolate"
},
"files": [
"lib",
"bin"
],
"contributors": [
{
"name": "Charlie Robbins",
Expand Down Expand Up @@ -55,17 +59,21 @@
{
"name": "Jinkwon Lee",
"email": "master@bdyne.net"
},
{
"name": "BigBlueHat",
"email": "byoung@bigbluehat.com"
}
],
"dependencies": {
"colors": "1.0.3",
"optimist": "0.6.x",
"union": "~0.4.3",
"ecstatic": "~1.2",
"http-proxy": "^v1.11.1",
"portfinder": "0.4.x",
"corser": "~2.0.0",
"ecstatic": "^2.0.0",
"opener": "~1.4.0",
"corser": "~2.0.0"
"optimist": "0.6.x",
"portfinder": "^1.0.13",
"union": "~0.4.3"
},
"devDependencies": {
"common-style": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ vows.describe('http-server').addBatch({
topic: function () {
request('http://127.0.0.1:8081/root/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
'status code should be the endpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
Expand All @@ -108,7 +108,7 @@ vows.describe('http-server').addBatch({
topic: function () {
request('http://127.0.0.1:8081/file', this.callback);
},
'status code should be the enpoint code 200': function (res) {
'status code should be the endpoint code 200': function (res) {
assert.equal(res.statusCode, 200);
},
'and file content': {
Expand Down

0 comments on commit 2af9dee

Please sign in to comment.