Apache Cordova Serve Library

Clone this repo:
  1. 47a6a4e chore(npm): rebuilt package-lock to v2 (#49) by エリス · 4 weeks ago master
  2. 4f03d37 fix(main.js): color of 404 status in console logs (#46) by Raphael von der Grün · 10 months ago
  3. ecee077 fix(main.js): resolve deprecation warning on Node.js 12 (#45) by Zoltan Galantai Fekete · 10 months ago
  4. 2607e5b fix: pass mandatory args to platform API constructor (#44) by seamlink-aalves · 12 months ago
  5. ea89b6e chore(asf): Update GitHub repo metadata by Darryl Pogue · 1 year, 10 months ago

cordova-serve

NPM

Node CI

This module provides a JavaScript API to serve up a Cordova application in the browser.

API Example:

const cordovaServe = require('cordova-serve')();

cordovaServe.launchServer(options);
cordovaServe.servePlatform(platform, options);
cordovaServe.launchBrowser(options);

API Methods

launchServer()

Launches a local HTTP server.

Code Example:

const cordovaServe = require('cordova-serve')();

cordovaServe.launchServer(options).then(function () {
    const { server, port, root } = cordovaServe;
    ...
}, error => {
    console.log(`An error occurred: ${error}`);
});

Parameters:

  • options: described below in the following section “launchServer & servePlatform Options”.

Return:

Returns a resolved or rejected promise depending on if the server had launched successfully.

On a fulfilled promise, the following properties are available on the returned object:

PropertyDescription
serveThe Node http.Server instance.
rootThe root that was specified, or cwd if none specified.
portThe port that was used. (Either the requested port, the default port of 8000, or the incremented value of the chosen port when the chosen port is already in use).

servePlatform()

Launches a server that serves up any Cordova platform (e.g. browser, android, etc) from the current project.

Code Example:

const cordovaServe = require('cordova-serve')();

cordovaServe.servePlatform(platform, options).then(() => {
    const { server, port, projectRoot, root } = cordovaServe;
    ...
}, error => {
    console.log(`An error occurred: ${error}`);
});

Parameters:

  • options: described below in the following section “launchServer & servePlatform Options”.

Return:

Note that for 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.

Returns a resolved or rejected promise depending on if the server had launched successfully.

On a fulfilled promise, the following properties are available on the returned object:

PropertyDescription
serveThe Node http.Server instance.
rootThe requested platform's www folder.
projectRootThe root folder of the Cordova project.
portThe used port. requested port, the default port 8000, or incremented value of the chosen port when already in use).

launchBrowser()

Launches a browser window pointing to the specified URL.

Code Example:

const cordovaServe = require('cordova-serve')();

cordovaServe.launchBrowser(options).then(
  stdout => {
    console.log(`Browser was launched successfully: ${stdout}`);
  },
  error => {
    console.log(`An error occurred: ${error}`);
  }
);

Parameters:

  • options (optional):
OptionsDescription
urlThe URL to open in the browser.
targetThe browser identifier to launch. Valid identifier: chrome, chromium, firefox, ie, opera, safari. (Default: chrome.)

Return:

Returns a resolved or rejected promise depending on if the browser had launched successfully.

launchServer & servePlatform Options

The options object passed to launchServer() and servePlatform() supports the following values (all optional):

OptionsDescription
rootThe file path on the local file system that is used as the root for the server, for default mapping of URL path to the local file system path.
portThe port for the server. Note that if this port is already in use, it will be incremented until a free port is found.
routerAn ExpressJS router. If provided, this will be attached before default static handling.
noLogOutputIf true, all log output will be turned off.
noServerInfoIf true, the Static file server running on... message will not be outputed.
eventsAn EventEmitter to use for logging. If provided, logging will be output using events.emit('log', msg). If not provided, console.log() will be used. Note that nothing will be output in either case if noLogOutput is true.