cordova-plugin-webserver
Cordova plugin for localhost web server written in Kotlin and Ktor
Install plugin
cordova plugin add https://github.com/Qbix/cordova-plugin-webserver.git
Supported platforms
Let's start
- First step: In your index.js file add this line to start the web server
cordova.plugins.webServer.startServer(function(result) { console.log(result); }, function(error) { console.log(error); })
- Second step: Build and run your app. Voila, enjoy the web server!
Start Server
cordova.plugins.webServer.startServer(function(result) { console.log(result); }, function(error) { console.log(error); })
Stop Server
cordova.plugins.webServer.stopServer(function(result) { console.log(result); }, function(error) { console.log(error); })
Requests
Serving static content
GET request:
http://localhost:3005/static-content/{your path from assets directory}
Example with serving index.html:
http://localhost:3005/static-content/www/index.html
Executing Cordova methods
POST request:
curl -X POST -F service=CordovaServiceName -F action=CordovaMethod -F args=["Args"] http://localhost:3005/cordova-request
Example with common cordova request:
Q.Users.Cordova.Labels.get(["e"], function(data) { console.log(data); }, function(err) { console.log(err); })
equivalent to
curl -X POST -F service=QUsersCordova -F action=get -F args=[["e"]] http://localhost:3005/cordova-request
Common errors
- Not found - if static content is not found.
- Service not found - if the web server could not find the service or the structure of the post request was incorrect.
- Invalid action execution - if the web server could not find the action or the structure of the post request was incorrect.
- Gateway timeout - if the web server could not execute cordova request within the timeout.
- Different callbackId - when we run our Cordova request we generate a callbackId, if we get a callbackId different than our callbackId we return an error.
- Plugin manager is null - something wrong happened after initialization.
- Plugin response is null - if plugin response is null.