Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface HttpServer

Hierarchy

  • HttpServer

Index

Properties

cache

Cache properties of the HTTP server.

defaultCharSet

defaultCharSet: String

Default charset value.

hostName

hostName: String

Host name of the server.

ipAddress

ipAddress: String

IP address of the server.

port

port: Number

Port listened to by the server.

request

request: HTTPRequest

The current HTTP Request Object.

response

response: HTTPResponse

The current HTTP Response Object.

ssl

SSL properties of the server.

started

started: Boolean

Return true if the HTTP Server is started.

Methods

addRequestHandler

  • addRequestHandler(pattern: String, modulePath: String, functionName: String): void
  • Adds a request handler function on the server. It is recommended to declare all request handler in the bootstrap.js file in order to be available at server start up.

    Step 1: Add a request handler

    // It is recommended to write these lines in bootstrap.js
    // On every "/ping" requests, call "pong()" function in "request-greetings" module
    httpServer.addRequestHandler('^/ping$', 'request-greetings', 'pong');

    Step 2: Handle the request

    // modules/request-greetings/index.js
    exports.pong = function pong( request, response ){
        return 'pong';
    }

    Parameters

    • pattern: String

      Regexp pattern to intercept a HTTP request

    • modulePath: String

      Path to the module that exports the functionName

    • functionName: String

      Function name which handles the request and returns the request response

    Returns void

addWebSocketHandler

  • addWebSocketHandler(pattern: String, filePath: String, socketID: String, sharedWorker: Boolean): void
  • Adds a WebSocket handler script on the server. It is recommended to declare all websocket handler in the bootstrap.js file in order to be available at server start up.

    Step 1: Add a websocket handler

    // It is recommended to write these lines in bootstrap.js
    httpServer.addWebSocketHandler('^/ping$', './websocket-greetings.js', 'websocket-id', true);

    Step 2: Handle the websocket

    // ./websocket-greetings.js
    // Same as for ShareWorker
    // Called every time a new websocket is connected
    onconnect = function ( event ) {
    
        // Get the websocket port
        var wsPort = event.ports[0];
    
        // Get the websocket handshake data
        var client = event.client;
        // Is available: client.ip, client.port, client.urlPath, client.headers
    
        // Called every time a client sends a message
        wsPort.onmessage = function( message ) {
    
            // Process data send by the client
            if ( message.data == 'hello' ){
                console.log( 'websocket data received: '+ message.data );
                // Send a response to client
                wsPort.postMessage( 'Hello back !' );
            }else{
                console.log( 'websocket data skipped: '+ JSON.stringify(message) );
            }
        };
    
        // Called when the socket receives an error
        wsPort.onerror = function() {
            // Handle websocket errors
        };
    
        // Called when the socket is closed
        wsPort.onclose = function() {
            // Do nothing and wait for another websocket connection
        };
    };

    Parameters

    • pattern: String

      Regexp pattern to intercept a WS request

    • filePath: String

      Absolute or relative path from the project to the file that defines the websocket handler. Filesystem are not working in filePath parameter (PROJECT, SOLUTION, ...).

    • socketID: String

      Socket ID usefull for removeWebSocketHandler()

    • sharedWorker: Boolean

      true if uses shared worker (recommended). false if uses dedicated worker.

    Returns void

removeRequestHandler

  • removeRequestHandler(pattern: String, modulePath: String, functionName: String): void
  • Removes an existing request handler function on the server.

    // Must match parameters of "addRequestHandler()"
    // httpServer.addRequestHandler('^/ping$', 'request-greetings', 'pong');
    httpServer.removeRequestHandler('^/ping$', 'request-greetings', 'pong');

    Parameters

    • pattern: String

      Regexp pattern to intercept a HTTP request

    • modulePath: String

      Path to the module that exports the functionName

    • functionName: String

      Function name which handles the request

    Returns void

removeWebSocketHandler

  • removeWebSocketHandler(socketID: String): void
  • Removes an existing websocket handler on the server.

    // Must match socketID parameter of "addWebSocketHandler()"
    // httpServer.addWebSocketHandler('^/ping$', 'backend/websocket-greetings.js', 'websocket-id', true);
    httpServer.removeWebSocketHandler( 'websocket-id' );

    Parameters

    • socketID: String

      Identifies the websocket to remove

    Returns void

start

  • start(): void

stop

  • stop(): void