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):
response.write()
, or determine a custom local file path and call serveFile()
to serve it up, or do no processing and call serveFile
with no params to treat urlPath
as relative to the root.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) and potentially directly handle serving up the resulting data. The signature of this method is as follows:
urlPathHandler(urlPath, request, response, do302, do404, serveFile)
Parameters:
url.parse(request.url).pathname
.urlPathHandler()
doesn't write the response itself, it should call this method either passing it the local file path to be served, or passing it nothing. In the latter case, it will treat urlPath
as relative to the root.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()
).