HttpServer

Description

This object was introduced in InCore 2.8.

› Inherits:

Object

Overview

Properties

Methods

Signals

Enumerations

Properties

error

This property holds the most recently occurred error or HttpServer.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:

Error

› Signal:

errorChanged()

› Attributes:

Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:

String

› Signal:

errorStringChanged()

› Attributes:

Readonly

localHost

This property holds whether the server should listen for incoming connections on the local loopback interface only. If set to true the server will not be reachable by other hosts on the network but internal clients such as docker containers (DockerContainer) only.

› Type:

Boolean

› Default:

false

› Signal:

localHostChanged()

› Attributes:

Writable

port

This property holds the port which to listen at.

› Type:

SignedInteger

› Default:

80

› Signal:

portChanged()

› Attributes:

Writable

routes

This property holds a list of header objects to use when sending a HTTP request.

› Type:

List<HttpServerRoute>

› Signal:

routesChanged()

› Attributes:

Readonly

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

routesDataChanged(SignedInteger index)

This signal is emitted whenever the List.dataChanged() signal is emitted, i.e. the item at index in the routes list itself emitted the dataChanged() signal.

Enumerations

Error

This enumeration describes all errors which can occur in HttpServer objects. The most recently occurred error is stored in the error property.

Name

Value

Description

HttpServer.NoError

0

No error occurred or was detected.

HttpServer.ListenError

1

Failed to listen at the specified port.

Example

import InCore.Foundation 2.9
import InCore.Http 2.9

Application {
    HttpServer {
        port: 3000
        HttpServerRoute {
            path: "/sum/<arg>/<arg>"
            function calcSum(a: int, b: int) {
                return "The sum of a and b is " + (a+b) + "\n"
            }
        }
        HttpServerRoute {
            path: "/item/<arg>"
            function getItem(id: string) {
                return "Data for item " + id + "\n"
            }
        }
        HttpServerRoute {
            path: "/item"
            method: HttpServerRequest.Post
            function createItem(request) {
                if (request.headers["content-type"] === "application/json")
                {
                    console.log("Creating new item with data:", JSON.stringify(JSON.parse(request.body)))
                    return [HttpServerResponse.Created, "da0805fb-5953-4156-ba29-df17ed6f57b4"]
                }
            }
        }
        HttpServerRoute {
            path: "/specialresponse"
            function test(request, response) {
                response.statusCode = HttpServerResponse.OK
                response.content.type = HttpContent.Json
                response.content.data = {"foo": 123, "bar": 456}
            }
        }
        HttpServerRoute {
            id: chunkedRoute
            asynchronous: true
            path: "/chunked"
            function getItem(request, responder) {
                responder.writeStatus(HttpServerResponse.OK)
                responder.writeHeaders({"Content-Type":"text/plain"})
                chunkWriteTimer.responder = responder
                chunkWriteTimer.x = 0
                chunkWriteTimer.start()
            }
        }
    }

    Timer {
        id: chunkWriteTimer
        property var responder: null
        property int x: 0
        interval: 1
        running: false
        onTriggered: {
            if (responder)
            {
                responder.writeChunk(("%1\n").arg(x++))
                if (x > 999)
                {
                    running = false
                    responder.finish()
                }
            }
        }
    }
}