From 3cd8fc34e3ab50cf7e996ab70918a08c14e186f0 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 4 Oct 2015 16:32:58 +0200 Subject: [PATCH] [minor] Add ability to specify the bind address --- README.md | 5 +++-- index.js | 9 +++++---- test/create-server.test.js | 17 +++++++++++++++++ test/mocha.opts | 2 -- 4 files changed, 25 insertions(+), 8 deletions(-) delete mode 100644 test/mocha.opts diff --git a/README.md b/README.md index 9436ecc..5254486 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -65,4 +66,4 @@ keys as option. ## License -MIT +[MIT](LICENSE) diff --git a/index.js b/index.js index 5be252a..4264713 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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. @@ -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); } diff --git a/test/create-server.test.js b/test/create-server.test.js index b7f3d73..e7578de 100644 --- a/test/create-server.test.js +++ b/test/create-server.test.js @@ -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'); diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index 3fbc269..0000000 --- a/test/mocha.opts +++ /dev/null @@ -1,2 +0,0 @@ ---reporter spec ---ui bdd