Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

Web Server

Improve this doc

This plugin allows you to start a local dynamic content web server for android and iOS devices.

Repo: https://github.com/bykof/cordova-plugin-webserver.git

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-webserver
    $ npm install --save @ionic-native/web-server@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { WebServer } from '@ionic-native/web-server';


constructor(private webServer: WebServer) { }

...

this.webServer.onRequest().subscribe(data => {
  console.log(data);
  const res: Response = {
    status: 200,
    body: '',
    headers: {
      'Content-Type': 'text/html'
    }
  };

  this.webServer.sendResponse(data.requestId, res)
    .catch((error: any) => console.error(error));
});

this.webServer.start(80)
  .catch((error: any) => console.error(error));

Instance Members

start(port)

This method will start your webserver.

Param Type Details
port number

Port number (default to 8080)

stop()

This method will stop your webserver.

onRequest()

This method returns an observable that streams HTTP requests to an observer.

Returns: Observable<Request> Returns an observable to resolve as a Request object

sendResponse(requestId, responseObject)

This method sends a response to a request.

Param Type Details
requestId string

Request ID to respond to

responseObject Response

Response object

Returns: Promise<any> Returns a promise that resolves when something happens

Response

Param Type Details
status number
body string
headers { [key: string]: string}

Request

Param Type Details
requestId string
body string
headers string
method 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE'
path string
query string

API

Native

General