adobe/brackets — pull request #6815 by andoband (2014-02-10 04:04:46+00:00)
Title: User can now specify a port for the live dev server

**Overview**
These changes allow a user to specify a fixed port number, via a preference, for the Live Development server. This fixes the the now-closed [Issue 5626](https://github.com/adobe/brackets/issues/5626).

**Use Cases**
I was struggling with the changing of port numbers while developing an application which used IndexedDB as well as the Google Drive API.

IndexedDB databases are host and port specific, thus any development data I stored while using one port number was lost each time I closed and reopened Brackets.

With the Google Drive API, applications must be served from hosts (and port) that the developer has registered through the API's console, thus I needed to be log into the Google API Console and update it each time Brackets was closed and reopened.

**Solution**
The StaticServer extension was the only area modified in order to support a user-specified port. StaticServer is Bracket's internal HTTP server, and is used solely by the LiveDevelopment module. Three sets of changes were necessary:

*StaticServerDomain.js*
StaticServerDomain, the actual server code executed in the node process, was updated to accept a port number in it's `getServer` command. If the passed in port isn't a valid port number or if the port number is already in use, StaticServerDomain will fallback to it's original behavior of serving from a random port. It may be desirable to log a warning when this happens, but I didn't code that.

The "listening" callback used when calling server.listen was factored out to a long-term listener now added via `server.on("listening", fn)`. This accommodates a second call to server.listen that appears in the `server.on("error", fn)` which could get called in the event that the specified port is already in use.

It's important to note that the public API for getServer has changed from `getServer(host, cb)` to `getServer(host, port, cb)`. There was only one place in core that needed updating for this as well as the unit tests, but this would break anyone else's extensions that explicitely call getServer.

*StaticServer.js*
StaticServer is the Bracket-side wrapper around the node domain. It was changed to accept a port number in the config object that is passed to it's constructor. StaticServer doesn't do anything with the port number other than pass it along to the node domain when calling getServer.

*main.js*
StaticServer's main.js pulls the user-specified port from preferences using the key "liveDevPort" and passes that value into the StaticServer constructor.

**Tests**
I added two tests. One tests that the specified port is used when it's passed in. The other tests that a random port is used if the specified port is already in use. The default case, when a port isn't specified by the user, is tested explicitly in an existing test.