Skip to content
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# create-server

[![Version npm](http://img.shields.io/npm/v/create-server.svg?style=flat-square)](http://browsenpm.org/package/create-server)[![Build Status](http://img.shields.io/travis/primus/create-server/master.svg?style=flat-square)](https://travis-ci.org/primus/create-server)[![Dependencies](https://img.shields.io/david/primus/create-server.svg?style=flat-square)](https://david-dm.org/primus/create-server)[![Coverage Status](http://img.shields.io/coveralls/primus/create-server/master.svg?style=flat-square)](https://coveralls.io/r/primus/create-server?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=primus)
[![Version npm](https://img.shields.io/npm/v/create-server.svg?style=flat-square)](http://browsenpm.org/package/create-server)[![Build Status](https://img.shields.io/travis/primus/create-server/master.svg?style=flat-square)](https://travis-ci.org/primus/create-server)[![Dependencies](https://img.shields.io/david/primus/create-server.svg?style=flat-square)](https://david-dm.org/primus/create-server)[![Coverage Status](https://img.shields.io/coveralls/primus/create-server/master.svg?style=flat-square)](https://coveralls.io/r/primus/create-server?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=primus)

I've found my self writing this particular piece of snippet over and over again.
If you need to have a common API for creating a HTTP, HTTPS or SPDY server this
Expand Down Expand Up @@ -36,6 +36,7 @@ The following properties can be configured:

- **port**: The port number we should listen on. Also used to determine which
type of server we need to create.
- **hostname**: What interface we should listen on.
- **spdy**: Create SPDY server instead of a HTTPS server.
- **root**: The root folder that contains your HTTPS certs.
- **key, cert, ca, pfx, crl** Path or array of paths which will be read out the
Expand Down Expand Up @@ -65,4 +66,4 @@ keys as option.

## License

MIT
[MIT](LICENSE)
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ function create(server, fn) {

fn = create.fns(fn || options);

var port = options.port || 443 // Force HTTPS by default.
, certs = options.key && options.cert // Check HTTPS certs.
var certs = options.key && options.cert // Check HTTPS certs.
, hostname = options.hostname // Bind address.
, port = options.port || 443 // Force HTTPS by default.
, secure = certs || 443 === port // Check for true HTTPS.
, spdy = 'spdy' in options; // Or are we spdy.

Expand Down Expand Up @@ -137,7 +138,7 @@ function create(server, fn) {
);

res.end('');
}).listen(+options.redirect);
}).listen(+options.redirect, hostname);

//
// Close the redirect server when the main server is closed.
Expand All @@ -162,7 +163,7 @@ function create(server, fn) {
if (fn[type]) fn[type]();

if (options.listen !== false) {
listen(server, port, fn.listening);
listen(server, port, hostname, fn.listening);
} else if (fn.listening) {
server.once('listening', fn.listening);
}
Expand Down
17 changes: 17 additions & 0 deletions test/create-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('create server', function () {
assume(server).to.be.instanceOf(http.Server);
});

it('allows to specify the bind address', function (next) {
server = create({
hostname: 'localhost',
port: ++port
}, {
listening: function (err) {
if (err) return next(err);

var address = server.address();

assume(address.address).to.equal('127.0.0.1');
assume(address.port).to.equal(port);
next();
}
});
});

it('proxies errors to the listener', function (next) {
server = create(80, { listening: function (err) {
if (!err) throw new Error('Port 80 should be restricted, we are not root');
Expand Down
2 changes: 0 additions & 2 deletions test/mocha.opts

This file was deleted.