{ } qjs-lws

HTTP mounts

A mount maps a URL prefix on a vhost to a backend: a directory of static files, a JS protocol callback, a redirect, or a CGI script. Mounts are passed via the mounts property of LWSContext's or LWSVhost's info object. Implemented in lws-mount.c (lwsjs_mount_from, lwsjs_mounts_from).

Shape

mounts can be one of:

// Array of mount objects.
mounts: [
  { mountpoint: '/static', origin: './public', def: 'index.html', originProtocol: LWSMPRO_FILE },
  { mountpoint: '/api',    protocol: 'http',                       originProtocol: LWSMPRO_CALLBACK },
]

// An object keyed by mountpoint.
mounts: {
  '/static': { origin: './public', def: 'index.html', originProtocol: LWSMPRO_FILE },
  '/api':    { protocol: 'http', originProtocol: LWSMPRO_CALLBACK },
}

// Short tuple form.
mounts: [
  ['/static', './public', 'index.html', 'http', null /* basic_auth_login_file */],
]

Mount object properties

PropertyC fieldNotes
mountpointmountpointURL prefix to match (e.g. '/', '/ws')
originoriginFilesystem path, hostname, or URL depending on originProtocol
defdefDefault file when the URL ends with /
protocolprotocolProtocol name to bind for LWSMPRO_CALLBACK / LWSMPRO_NO_MOUNT
cgienvcgienvArray/object of {name, value} for CGI environment
extraMimetypes / extra_mimetypesextra_mimetypesExtra extension→mimetype map (see below)
interpretinterpretExtension→CGI interpreter map
cgiTimeout / cgi_timeoutcgi_timeoutCGI timeout seconds
cacheMaxAge / cache_max_agecache_max_ageCache-Control max-age
authMask / auth_maskauth_maskAuth bitmask
cacheReusable / cache_reusablecache_reusablebool
cacheRevalidate / cache_revalidatecache_revalidatebool
cacheIntermediaries / cache_intermediariescache_intermediariesbool
originProtocol / origin_protocolorigin_protocolOne of LWSMPRO_*
basicAuthLoginFile / basic_auth_login_filebasic_auth_login_filehtpasswd-style file

origin_protocol values:

ConstantMeaning
LWSMPRO_HTTPProxy to another HTTP host (origin = host[/path])
LWSMPRO_HTTPSSame, over TLS
LWSMPRO_FILEServe files from a local directory (origin = path)
LWSMPRO_CGIRun as CGI
LWSMPRO_REDIR_HTTP301/302 redirect to an http URL
LWSMPRO_REDIR_HTTPSSame, https
LWSMPRO_CALLBACKDispatch to a protocol's HTTP callbacks
LWSMPRO_NO_MOUNTReserve the prefix without serving (e.g. websocket-only)

Vhost-options chains

cgienv, extraMimetypes, interpret, headers, pvo, and rejectServiceKeywords are all struct lws_protocol_vhost_options chains. Inputs accepted (see lwsjs_vhost_options_from / lwsjs_vhost_option_from in lws-vhost.c):

  • An array of { name, value, options?, next? } objects.
  • A single object — used as the head of the chain.
  • A tuple [name, value, options].

The name/value strings are duplicated into a private chain and freed on context destroy.

Example extraMimetypes:

mounts: [{
  mountpoint: '/',
  origin: '.',
  def: 'README.md',
  originProtocol: LWSMPRO_FILE,
  extraMimetypes: [
    { name: '.md',  value: 'text/markdown' },
    { name: '.wasm', value: 'application/wasm' },
  ],
}],

Common mount patterns

Static files

{ mountpoint: '/', origin: './public', def: 'index.html', originProtocol: LWSMPRO_FILE }

Reverse proxy

{ mountpoint: '/warmcat', origin: 'warmcat.com/', def: 'index.html', originProtocol: LWSMPRO_HTTP }

Dispatch to a JS protocol

{ mountpoint: '/api', protocol: 'http', originProtocol: LWSMPRO_CALLBACK }

Inside protocol 'http', onHttp(wsi, uri) is invoked for any URL beginning with /api.

WebSocket endpoint (no HTTP serving)

{ mountpoint: '/ws', protocol: 'ws', originProtocol: LWSMPRO_NO_MOUNT }

Combine with a protocol 'ws' that implements onEstablished, onReceive, etc.

Redirect

{ mountpoint: '/old', origin: 'newsite.example/x', originProtocol: LWSMPRO_REDIR_HTTPS }