commit | 674b245dff21c08c0bd27eb3c17c1f8059afc9eb | [log] [tgz] |
---|---|---|
author | Tim Barham <tim.barham@microsoft.com> | Fri Jun 05 12:05:52 2015 -0700 |
committer | Tim Barham <tim.barham@microsoft.com> | Tue Jun 09 17:27:33 2015 -0700 |
tree | f18803fea4a4ed6e5ba88201068613b505a39488 |
CB-9127 Implements cordova-serve module. This takes code and functionality from cordova-browser and cordova-lib ('cordova serve' command) and combines it in a single module. Most of the code is lifted directly from those two places. Once this is checked in and we have a release, cordova-lib and cordova-browser can be updated to use it.
This module can be used to serve up a Cordova application in the browser. It has no command-line, but rather is intended to be called using the following API:
var serve = require('cordova-serve'); serve.launchServer(opts); serve.servePlatform(platform, opts); serve.launchBrowser(ops); serve.sendStream(filePath, request, response[, readStream][, noCache]);
launchServer(opts);
Launches a server with the specified options. Parameters:
servePlatform(platform, opts);
Launches a server that serves up any Cordova platform (e.g. browser
, android
etc) from the current project. Parameters:
servePlatform()
, the root
value should be a Cordova project‘s root folder, or any folder within it - servePlatform()
will replace it with the platform’s www_dir
folder. If this value is not specified, the cwd will be used.launchBrowser(opts);
Launches a browser window pointing to the specified URL. The single parameter is an options object that supports the following values (both optional):
chrome
, chromium
, firefox
, ie
, opera
, safari
. If no browser is specified,sendStream(filePath, request, response[, readStream][, noCache]);
The server uses this method to stream files, and it is provided as a convenience method you can use if you are customizing the stream by specifying opts.streamHandler
. Parameters:
streamHandler
).streamHandler
).streamHandler
).The opts object passed to launchServer()
and servePlatform()
supports the following values (all optional):
cordova-serve
will by default treat the URL as relative to the platform's www_dir
, but will first call this method, if provided, to support custom handling.cordova-serve
will by default stream files using sendStream()
, described above, which just streams files, but will first call this method, if provided, to support custom streaming. This method is described in more detail below.Provide this method if you need to do custom processing of URL paths. That is, custom mapping of URL path to local file path. The signature of this method is as follows:
urlPathProcessor(urlPath, request, response, do302, do404)
Parameters:
url.parse(request.url).pathname
.Return value:
Broadly, there are three possible actions you can take in your urlPathProcessor
handler:
response.end()
. In this scenario, you should return null
.{filePath: <value>}
, where <value>
is the local file path.{filePath: null}
.Provide this method if you wish to perform custom stream handling. The signature of this method is as follows:
streamHandler(filePath, request, response)
Parameters:
sendStream()
, it is useful if even a fake file name has a file extension, as that is used for mime type lookup.Return value:
Return true
if you have handled the stream request, otherwise false
.
If you provide this method, it will be called as soon as the server is created. It allows you to attach additional functionality to the server, such has event handlers, web sockets etc. The signature of this method is as follows:
serverExtender(server, root)
Parameters:
http.createServer()
).