Skip to content

Commit 211a260

Browse files
authored
Allow the user to specify port. (#474)
Fixes #464
1 parent 2f59200 commit 211a260

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

lib/commands/serve.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ module.exports.description = 'generate, update, and display HTML documentation';
1616
*/
1717
module.exports.parseArgs = function (yargs) {
1818
return sharedOptions.sharedOutputOptions(
19-
sharedOptions.sharedInputOptions(yargs));
19+
sharedOptions.sharedInputOptions(yargs))
20+
.option('port', {
21+
describe: 'port for the local server',
22+
type: 'number',
23+
default: 4001
24+
});
2025
};
2126

22-
var server = new Server();
23-
server.on('listening', function () {
24-
process.stdout.write('documentation.js serving on port 4001\n');
25-
});
26-
2727
/**
2828
* Wrap the documentation build command along with a server, making it possible
2929
* to preview changes live
@@ -33,6 +33,10 @@ server.on('listening', function () {
3333
* @returns {undefined} has side effects
3434
*/
3535
function serve(documentation, parsedArgs) {
36+
var server = new Server(parsedArgs.options.port);
37+
server.on('listening', function () {
38+
process.stdout.write('documentation.js serving on port ' + parsedArgs.options.port + '\n');
39+
});
3640
parsedArgs.commandOptions.format = 'html';
3741
build(documentation, parsedArgs, function (err, output) {
3842
if (err) {

lib/serve/server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ var http = require('http'),
1111
* of files and notifies any browsers using LiveReload to reload
1212
* and display the new content.
1313
* @class
14+
* @param {number} port server port to serve on.
1415
*/
15-
function Server() {
16+
function Server(port) {
17+
if (typeof port !== 'number') {
18+
throw new Error('port argument required to initialize a server');
19+
}
20+
this._port = port;
1621
this._files = [];
1722
}
1823

@@ -79,7 +84,7 @@ Server.prototype.start = function (callback) {
7984
this._http = http.createServer(this.handler.bind(this));
8085

8186
this._lr.listen(35729, function () {
82-
this._http.listen(4001, function () {
87+
this._http.listen(this._port, function () {
8388
this.emit('listening');
8489
callback();
8590
}.bind(this));

test/bin-watch-serve.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ test('provides index.html', function (t) {
5353
});
5454
}, options);
5555

56+
test('accepts port argument', function (t) {
57+
var docProcess = documentation(['serve', 'fixture/simple.input.js', '--port=4004']);
58+
docProcess.stdout.on('data', function (data) {
59+
t.equal(data.toString().trim(), 'documentation.js serving on port 4004', 'shows listening message');
60+
get('http://localhost:4004/', function (text) {
61+
t.ok(text.match(/<html>/), 'sends an html index file');
62+
docProcess.kill();
63+
t.end();
64+
});
65+
});
66+
}, options);
67+
5668
test('--watch', function (t) {
5769
var tmpFile = path.join(os.tmpdir(), '/simple.js');
5870
fs.writeFileSync(tmpFile, '/** a function */function apples() {}');

test/lib/server.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ var indexFile = new File({
2626
contents: new Buffer('<html>')
2727
});
2828

29+
test('server - throws on bad port', function (t) {
30+
t.throws(function () {
31+
var server = new Server('4001');
32+
}, 'port must be a number');
33+
t.throws(function () {
34+
var server = new Server();
35+
}, 'port must be provided');
36+
t.end();
37+
});
38+
2939
test('server', function (t) {
30-
var server = new Server();
40+
var server = new Server(4001);
3141
t.ok(server, 'server is initialized');
3242
server.start(function () {
3343

0 commit comments

Comments
 (0)