Comparing version 0.1.4 to 0.1.5
64
index.js
@@ -15,3 +15,3 @@ module.exports = (function() { | ||
var Environ = { | ||
dirname: __dirname | ||
root: __dirname | ||
}; | ||
@@ -24,10 +24,2 @@ | ||
port: 8888, | ||
httpRoot: { | ||
default: function( dirname ) { | ||
return dirname + '/www' | ||
}, | ||
www: function( dirname ) { | ||
return dirname + '/www' | ||
} | ||
}, | ||
index: 'index.html', | ||
@@ -44,7 +36,5 @@ verbose: true, | ||
var options = (typeof arguments[0] === 'object' ? pop( arguments ) : {}); | ||
var callback = (typeof arguments[0] === 'function' ? pop( arguments ) : function() {}); | ||
//var callback = (typeof arguments[0] === 'function' ? pop( arguments ) : function() {}); | ||
function pop( subject ) { | ||
return Array.prototype.pop.call( subject ); | ||
} | ||
that._environ = Object.create( Environ ); | ||
@@ -64,5 +54,14 @@ var config = new Config(); | ||
that.protocol = 'https'; | ||
that.port = (that.port === 80 && !options.port ? 443 : that.port); | ||
that.ssl = that._getSSLCerts( that.ssl.key , that.ssl.cert ); | ||
} | ||
var httpRoot = options.httpRoot ? Object.create( options.httpRoot ) : {}; | ||
Object.defineProperty( that , 'httpRoot' , { | ||
get: function() { | ||
return httpRoot; | ||
} | ||
}); | ||
var toUse = []; | ||
@@ -83,2 +82,5 @@ | ||
}); | ||
// add the default httpRoot | ||
that.setHttpDir( 'default' , '/www' ); | ||
} | ||
@@ -94,2 +96,12 @@ | ||
environ: function( key , value ) { | ||
this._environ[key] = value; | ||
return this; | ||
}, | ||
setHttpDir: function( domain , value ) { | ||
this.httpRoot[domain] = value; | ||
return this; | ||
}, | ||
_getSSLCerts: function( key , cert ) { | ||
@@ -118,2 +130,4 @@ return { | ||
util.puts( 'server running at ' + that.protocol + '://localhost:' + ( that.port ) + '/' ); | ||
return that; | ||
}, | ||
@@ -123,2 +137,3 @@ | ||
this.toUse.push( handler ); | ||
return this; | ||
}, | ||
@@ -128,9 +143,17 @@ | ||
if (typeof this.httpRoot === 'string') { | ||
return Environ.dirname + this.httpRoot; | ||
var that = this; | ||
var root = that._environ.root; | ||
if (typeof that.httpRoot === 'string') { | ||
return root + that.httpRoot; | ||
} | ||
var getter = (this.httpRoot[subdomain] || this.httpRoot.default); | ||
return typeof getter === 'function' ? getter( Environ.dirname ) : __dirname; | ||
var httpRoot = (that.httpRoot[subdomain] || that.httpRoot.default); | ||
if (typeof httpRoot === 'function') { | ||
return httpRoot( root ); | ||
} | ||
else { | ||
return root + httpRoot; | ||
} | ||
}, | ||
@@ -246,2 +269,7 @@ | ||
function pop( subject ) { | ||
return Array.prototype.pop.call( subject ); | ||
} | ||
function pointsToDirectory( path ) { | ||
@@ -248,0 +276,0 @@ return (/(\/$)|(\.[0-9a-z]+$)/i).test( path ) === false; |
{ | ||
"name": "httpd-node", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A super simple HTTPD server for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
112
README.md
httpd-node | ||
========== | ||
A super simple HTTPD server for node.js | ||
#### A super simple HTTPD server for node.js | ||
Overview | ||
----- | ||
__httpd-node__ is a simple HTTPD that includes support for ssl and multiple subdomains. | ||
Installation | ||
@@ -17,15 +22,116 @@ ----- | ||
npm start | ||
The standalone config can be found in [standalone.js](standalone.js). | ||
### Module | ||
### Requiring | ||
```javascript | ||
var httpd = require( 'httpd-node' ); | ||
``` | ||
### Setting Up the Environment | ||
```javascript | ||
httpd.environ( 'root' , '/path/to/your/public/directory' ); | ||
``` | ||
### Creating an Instance | ||
An options object can be passed to the httpd constructor: | ||
```javascript | ||
var server = new httpd( options ); | ||
``` | ||
| Parameter | Type | Default | Description | | ||
| --------- | ---- | ------- | ----------- | | ||
| `port` | `Integer` | `8888` | The port for this instance. | | ||
| `index` | `String` | `'index.html'` | The name of the file that should be served when a directory is requested. | | ||
| `verbose` | `Boolean` | `true` | When verbose is true, the http response code and request path will be logged to the console. | | ||
| `ssl` | `Object` | `null` | An object containing paths to ssl .key and .cert files | | ||
Examples | ||
----- | ||
First, require httpd and setup your environment: | ||
```javascript | ||
var httpd = require( 'httpd-node' ); | ||
httpd.environ( 'root' , '/path/to/your/public/directory' ); | ||
``` | ||
### Basic | ||
Assuming your public directory contains a www directory, all you need to get started is: | ||
```javascript | ||
var server = new httpd(); | ||
server.start(); | ||
``` | ||
### Subdomains | ||
Point yourdomain.com and rad.yourdomain.com to different directories: | ||
```javascript | ||
var server = new httpd(); | ||
server.setHttpDir( 'default' , '/cool' ); | ||
server.setHttpDir( 'rad' , '/rad' ); | ||
server.start(); | ||
``` | ||
### SSL | ||
HTTPS on port 8080: | ||
```javascript | ||
var server = new httpd({ | ||
port: 8080, | ||
ssl: { | ||
key: '/absolute/path/to/ssl/key.key', | ||
cert: '/absolute/path/to/ssl/cert.crt' | ||
} | ||
}); | ||
server.start(); | ||
``` | ||
Methods | ||
----- | ||
### server.setHttpDir | ||
- Adds a new http directory and subdomain. The default directory is `/www`. | ||
```javascript | ||
server.setHttpDir( 'www' , '/www' ); | ||
server.setHttpDir( 'cdn' , '/cdn' ); | ||
// to override the default | ||
server.setHttpDir( 'default' , '/some_other_path' ); | ||
``` | ||
### server.use | ||
- Adds a callback that will be executed before the response is sent. | ||
- `data` is an object containing subdomain, httpRoot, and request path. | ||
```javascript | ||
server.use(function( request , response , data ) { | ||
// do stuff here | ||
}); | ||
``` | ||
### server.environ | ||
- Same as [httpd.environ](#setting-up-the-environment), but sets the environment for the server instance rather than the default httpd environment. | ||
```javascript | ||
server.environ( 'root' , '/path/to/your/public/directory' ); | ||
``` | ||
### server.start | ||
- Starts the httpd instance. | ||
```javascript | ||
server.start(); | ||
``` | ||
``` |
@@ -13,21 +13,2 @@ (function() { | ||
}()); | ||
}()); |
15922
268
137