server (desktop)

Since 2.0.0

Constants

Header : map =

74 elements
{
  ACCEPT=Accept,
  ACCEPT_CHARSET=Accept-Charset,
  ACCEPT_ENCODING=Accept-Encoding,
  ACCEPT_LANGUAGE=Accept-Language,
  ACCEPT_RANGES=Accept-Ranges,
  ACCESS_CONTROL_ALLOW_CREDENTIALS=Access-Control-Allow-Credentials,
  ACCESS_CONTROL_ALLOW_HEADERS=Access-Control-Allow-Headers,
  ACCESS_CONTROL_ALLOW_METHODS=Access-Control-Allow-Methods,
  ACCESS_CONTROL_ALLOW_ORIGIN=Access-Control-Allow-Origin,
  ACCESS_CONTROL_EXPOSE_HEADERS=Access-Control-Expose-Headers,
  ACCESS_CONTROL_MAX_AGE=Access-Control-Max-Age,
  ACCESS_CONTROL_REQUEST_HEADERS=Access-Control-Request-Headers,
  ACCESS_CONTROL_REQUEST_METHOD=Access-Control-Request-Method,
  AGE=Age,
  ALLOW=Allow,
  AUTHORIZATION=Authorization,
  CACHE_CONTROL=Cache-Control,
  CLEAR_SITE_DATA=Clear-Site-Data,
  CONNECTION=Connection,
  CONTENT_DISPOSITION=Content-Disposition,
  CONTENT_ENCODING=Content-Encoding,
  CONTENT_LANGUAGE=Content-Language,
  CONTENT_LENGTH=Content-Length,
  CONTENT_LOCATION=Content-Location,
  CONTENT_RANGE=Content-Range,
  CONTENT_SECURITY_POLICY=Content-Security-Policy,
  CONTENT_TYPE=Content-Type,
  COOKIE=Cookie,
  CROSS_ORIGIN_EMBEDDER_POLICY=Cross-Origin-Embedder-Policy,
  CROSS_ORIGIN_OPENER_POLICY=Cross-Origin-Opener-Policy,
  CROSS_ORIGIN_RESOURCE_POLICY=Cross-Origin-Resource-Policy,
  DATE=Date,
  ETAG=ETag,
  EXPECT=Expect,
  EXPIRES=Expires,
  FROM=From,
  HOST=Host,
  IF_MATCH=If-Match,
  IF_MODIFIED_SINCE=If-Modified-Since,
  IF_NONE_MATCH=If-None-Match,
  IF_RANGE=If-Range,
  IF_UNMODIFIED_SINCE=If-Unmodified-Since,
  LAST_MODIFIED=Last-Modified,
  LINK=Link,
  LOCATION=Location,
  MAX_FORWARDS=Max-Forwards,
  ORIGIN=Origin,
  PRAGMA=Pragma,
  PROXY_AUTHENTICATE=Proxy-Authenticate,
  PROXY_AUTHORIZATION=Proxy-Authorization,
  RANGE=Range,
  REFERER=Referer,
  REFERRER_POLICY=Referrer-Policy,
  RETRY_AFTER=Retry-After,
  SEC_WEBSOCKET_KEY=Sec-WebSocket-Key,
  SERVER=Server,
  SET_COOKIE=Set-Cookie,
  STRICT_TRANSPORT_SECURITY=Strict-Transport-Security,
  TE=TE,
  TRAILER=Trailer,
  TRANSFER_ENCODING=Transfer-Encoding,
  UPGRADE=Upgrade,
  USER_AGENT=User-Agent,
  VARY=Vary,
  VIA=Via,
  WARNING=Warning,
  WWW_AUTHENTICATE=WWW-Authenticate,
  X_ACCEL_BUFFERING=X-Accel-Buffering,
  X_CONTENT_TYPE_OPTIONS=X-Content-Type-Options,
  X_FORWARDED_FOR=X-Forwarded-For,
  X_FORWARDED_PROTO=X-Forwarded-Proto,
  X_FRAME_OPTIONS=X-Frame-Options,
  X_HTTP_METHOD_OVERRIDE=X-HTTP-Method-Override,
  X_PERMITTED_CROSS_DOMAIN_POLICIES=X-Permitted-Cross-Domain-Policies
}

Functions

newServer(config = {}) — Initializes server using provided config. Returns ServerValue.

use std, server

newServer()
  .get("/", def(ctx) = ctx.json({"message": "Hello, world!"}))
  .start(8081)

serve(port = 8080, dir = ".") — Starts a server on the port and hosts the directory dir

use server
serve(8083, "./public_html")

Types

ServerValue

Functions

get(path, handler, roles...) — adds a GET request handler

post(path, handler, roles...) — adds a POST request handler

put(path, handler, roles...) — adds a PUT request handler

patch(path, handler, roles...) — adds a PATCH request handler

head(path, handler, roles...) — adds a HEAD request handler

delete(path, handler, roles...) — adds a DELETE request handler

options(path, handler, roles...) — adds a OPTIONS request handler

error(status, handler, contentType = "*") — adds an error handler

exception(className, handler) — adds an exception handler

start(port = 8080, host = "") — Starts a server. Use port 0 to start a server on a random port.

stop() — Stops a server

ContextValue

Functions

appData(key) — gets an appData value by key. See config.appData

attribute(key, value = "") — gets or sets an attribute by key

basicAuthCredentials() — returns a basic authorization credentials, an array with two elements — username and password

extract(username, password) = ctx.basicAuthCredentials()

body() — returns a response body as a string

bodyAsBytes() — returns a response body as a byte array

characterEncoding() — returns a character encoding from Content-Type if possible

cookie(name, value = "", maxAge = -1) — gets or sets a cookie

contentLength() — returns a content length in bytes

contentType(contentType = "") — gets or sets a Content-Type header

contextPath() — returns a request context path

endpointHandlerPath() — returns a matched endpoint handler path

formParam(key) — returns a form parameter

fullUrl() — returns a full url

handlerType() — returns a current handler type

header(name, value ="") — gets or sets header

host() — returns a host

html(html) — sets result to the specified html string. Also sets Content-Type header to text/html

ip() — returns an IP address

isHttpMethod() — returns true if the request is http method

isMultipartFormData() — returns true if the request is multipart/formdata

isMultipart() — returns true if the request is multipart

json(obj) — serializes an object to json string and sets it as the result

jsonStream(obj) — serializes an object to json stream and sets it as the result

matchedPath() — returns a matched request path

method() — returns a method (GET, POST, ...)

path() — returns a request path

pathParam(key) — returns a request path parameter

port() — returns a port number

protocol() — returns a protocol

queryParam(key) — returns a query parameter

queryString() — returns a query string

redirect(location, statusCode = 302) — redirects to a location with the given status. By default, the status is 302 FOUND

removeCookie(name, path = "/") — removes a cookie by name and path

render(filePath, data = {}) — renders a file with specified data and sets it as the result

result(value = "") — gets or sets a result. value can be a string or a byte array

status(status = ...) — gets or sets a status code. status can be an integer status code (404, 500) or a string status name ("NOT_FOUND", "INTERNAL_SERVER_ERROR").

statusCode() — returns a response status code

scheme() — returns a request scheme

url() — returns a request url

userAgent() — returns an User-Agent header

Config

{
  "webjars": true,
  "classpathDirs": ["dir1", "dir2"],
  "externalDirs": ["dir1", "dir2"],

  "asyncTimeout": 6_000,
  "defaultContentType": "text/plain",
  "etags": true,
  "maxRequestSize": 1_000_000,

  "defaultHost": "localhost",
  "defaultPort": 8000,

  "caseInsensitiveRoutes": true,
  "ignoreTrailingSlashes": true,
  "multipleSlashesAsSingle": true,
  "contextPath": "/",

  "basicAuth": ["user", "password"],
  "dev": true,
  "showBanner": false,
  "sslRedirects": true,
  "virtualThreads": true,
  "appData": {
    "key1": "value1",
    "key2": "value2"
  }
}