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 |
hasServiceFunction(request_data: Object): boolean 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 Members
public http2: * source
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:
Name | Type | Attribute | Description |
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.
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:
Name | Type | Attribute | Description |
request | http.IncomingMessage | given by the 'request'-event of the server |
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:
Name | Type | Attribute | Description |
request_data | Object | analyzed representation of the request |
|
response | http.ServerResponse | given by the 'request'-event of the server |
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:
Name | Type | Attribute | Description |
request_data | Object | analyzed representation of the request |
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:
Name | Type | Attribute | Description |
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'