meteor/cordova-httpd

Name: cordova-httpd

Owner: Meteor

Description: CorHttpd is a plugin to embed a tiny web server into Cordova

Created: 2014-09-09 06:17:12.0

Updated: 2015-02-17 20:50:24.0

Pushed: 2014-09-25 06:35:48.0

Homepage:

Size: 496

Language: Objective-C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

CorHttpd: embeded httpd for cordova

Supported platform:

You can browse (locally or remotely) to access files in android/ios phone/pad:

Why http access is good?

How to use CorHttpd?

Add the plugin to your cordova project:

cordova plugin add https://github.com/floatinghotpot/cordova-httpd.git

Quick start, copy the demo files, and just build to play.

cp -r plugins/com.rjfun.cordova.httpd/test/* www/
Javascript APIs
tServer( options, success_callback, error_callback );

Server( success_callback, error_callback );

RL( success_callback, error_callback );

ocalPath( success_callback, error_callback );

Example code: (read the comments)

var httpd = null;
function onDeviceReady() {
    httpd = ( cordova && cordova.plugins && cordova.plugins.CorHttpd ) ? cordova.plugins.CorHttpd : null;
}
function updateStatus() {
    document.getElementById('location').innerHTML = "document.location.href: " + document.location.href;
    if( httpd ) {
      /* use this function to get status of httpd
      * if server is up, it will return http://<server's ip>:port/
      * if server is down, it will return empty string ""
      */
        httpd.getURL(function(url){
            if(url.length > 0) {
                document.getElementById('url').innerHTML = "server is up: <a href='" + url + "' target='_blank'>" + url + "</a>";
            } else {
                document.getElementById('url').innerHTML = "server is down.";
            }
        });
        // call this function to retrieve the local path of the www root dir
        httpd.getLocalPath(function(path){
            document.getElementById('localpath').innerHTML = "<br/>localPath: " + path;
        });
    } else {
        alert('CorHttpd plugin not available/ready.');
    }
}
function startServer( wwwroot ) {
    if ( httpd ) {
        // before start, check whether its up or not
        httpd.getURL(function(url){
            if(url.length > 0) {
                document.getElementById('url').innerHTML = "server is up: <a href='" + url + "' target='_blank'>" + url + "</a>";
            } else {
                /* wwwroot is the root dir of web server, it can be absolute or relative path
                * if a relative path is given, it will be relative to cordova assets/www/ in APK.
                * "", by default, it will point to cordova assets/www/, it's good to use 'htdocs' for 'www/htdocs'
                * if a absolute path is given, it will access file system.
                * "/", set the root dir as the www root, it maybe a security issue, but very powerful to browse all dir
                */
                httpd.startServer({
                    'www_root' : wwwroot,
                    'port' : 8080
                }, function( url ){
                  // if server is up, it will return the url of http://<server ip>:port/
                  // the ip is the active network connection
                  // if no wifi or no cell, "127.0.0.1" will be returned.
                    document.getElementById('url').innerHTML = "server is started: <a href='" + url + "' target='_blank'>" + url + "</a>";
                }, function( error ){
                    document.getElementById('url').innerHTML = 'failed to start server: ' + error;
                });
            }

        });
    } else {
        alert('CorHttpd plugin not available/ready.');
    }
}
function stopServer() {
    if ( httpd ) {
        // call this API to stop web server
        httpd.stopServer(function(){
            document.getElementById('url').innerHTML = 'server is stopped.';
        },function( error ){
            document.getElementById('url').innerHTML = 'failed to stop server' + error;
        });
    } else {
        alert('CorHttpd plugin not available/ready.');
    }
}

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.