Home Reference Source Repository
import ServiceProvider from 'servingjs/src/ServiceProvider.js'
public class | source

ServiceProvider

Example:

import ServiceProvider = "node_modules/servingjs/build/ServiceProvider.js";
const manifest = {
    example_function(a,b) {
        return a + b;
    }
};
const options = {
    logging: false,
    isAuthorized() {
        return true; 
    }
};
const service_provider = new ServiceProvider(manifest, options);
const server = service_provider.startServer(8000);
// server.close();

Constructor Summary

Public Constructor
public

constructor(service_manifest: object, options: {logging: boolean, isAuthorized: function})

Constructs the ServiceProvider.

Member Summary

Public Members
public

http2: *

public

Determines whether the server should log to the console.

Method Summary

Public Methods
public

async handleRequest(request: http.IncomingMessage, response: http.ServerResponse)

This method handles requests made to the server.

public

startServer(port: number, options: Object): http.Http2Server | http.Http2SecureServer

This method provides a convenient way to start a server which uses this 'ServiceProvider'.

Protected Methods
protected

async analyzeRequest(request: http.IncomingMessage): Object

This method analyzes requests made to the server.

protected

checkRequest(request_data: Object, response: http.ServerResponse): boolean

This method is called when a request is analyzed.

protected

This method is called to determine whether a service function exists.

protected

async invokeServiceFunction(request_data: Object, response: http.ServerResponse)

This method is called when a request is valid and authorized and the requested service function exists.

protected

isAuthorized(service_function_name: string, credentials: {user: string, password: string}): boolean

This method is called to determine whether a request shall be authorized.

Public Constructors

public constructor(service_manifest: object, options: {logging: boolean, isAuthorized: function}) source

Constructs the ServiceProvider.

Params:

NameTypeAttributeDescription
service_manifest object

provides the service functions available via the server

options {logging: boolean, isAuthorized: function}

logging to console, authorization check function

Public Members

public http2: * source

public logging: boolean source

Determines whether the server should log to the console.

Public Methods

public async handleRequest(request: http.IncomingMessage, response: http.ServerResponse) source

This method handles requests made to the server. It analyzes the incoming request and responds with the appropriate HTTP status. It is meant to be the direct callback of 'createServer'.

Params:

NameTypeAttributeDescription
request http.IncomingMessage

given by the 'request'-event of the server

response http.ServerResponse

given by the 'request'-event of the server

public startServer(port: number, options: Object): http.Http2Server | http.Http2SecureServer source

This method provides a convenient way to start a server which uses this 'ServiceProvider'. If a certificate is provided then the server uses HTTPS otherwise HTTP.

Params:

NameTypeAttributeDescription
port number

the servers port

options Object

passed to the http 'createServer' function

Return:

http.Http2Server | http.Http2SecureServer

server - the started server instance

Protected Methods

protected async analyzeRequest(request: http.IncomingMessage): Object source

This method analyzes requests made to the server. It preprocesses the request to extract essential information.

Params:

NameTypeAttributeDescription
request http.IncomingMessage

given by the 'request'-event of the server

Return:

Object

a representation of the essential request properties

protected checkRequest(request_data: Object, response: http.ServerResponse): boolean source

This method is called when a request is analyzed. It handles the HTTP negotiations.

Params:

NameTypeAttributeDescription
request_data Object

analyzed representation of the request

response http.ServerResponse

given by the 'request'-event of the server

Return:

boolean

true - if and only if a POST request is accepted and well-formed

protected hasServiceFunction(request_data: Object): boolean source

This method is called to determine whether a service function exists. It checks if the manifest has a property which matches the service function's name and if it's a function.

Params:

NameTypeAttributeDescription
request_data Object

analyzed representation of the request

Return:

boolean

true - if and only if the requested service function exists (and is a function)

protected async invokeServiceFunction(request_data: Object, response: http.ServerResponse) source

This method is called when a request is valid and authorized and the requested service function exists. It invokes the requested service function and writes the result to the response body.

Params:

NameTypeAttributeDescription
request_data Object

analyzed representation of the request

response http.ServerResponse

given by the 'request'-event of the server

protected isAuthorized(service_function_name: string, credentials: {user: string, password: string}): boolean source

This method is called to determine whether a request shall be authorized. This is the default setting which authorizes all request. It is meant o be overwritten when necessary either by class extension or by the constructor 'options'

Params:

NameTypeAttributeDescription
service_function_name string

name or the requested service function

credentials {user: string, password: string}

credentials to authorize request

Return:

boolean

true - if and only if the request is authorized