aladov3.13.0

Docs/Reference

Main Contracts

InitializeApplicationOptions

Property Type Description
serverOptions AladoServerOptions Server configuration
controllers AnyClass[] | ControllerConfig[] Controller classes to register

AladoServerOptions

Property Type Default Description
port number — (required) HTTP port: a whole number from 0 to 65535 (0 picks a free port). Anything else, NaN included, throws at startup
host string '0.0.0.0' Address to listen on
backlog number 2048 Pending connection queue length
appId string — App identifier for multi-app setups
cors CorsOptions { enable: false } enable, allowedOrigin (one origin, a list, or '*' alone or in the list, or a function returning one of these; default '*'), allowedHeaders (default ['Content-Type', 'Authorization'], [] for none), exposeHeaders, allowedCredentials (with '*', each browser gets its own Origin back), maxAge (seconds, default 86400). See CORS
openApiDoc OpenApiDoc { enable: false } enable, route, info (both required when enabled; info.title also names the docs page: Alado | <title>). See OpenAPI / Swagger
headers Record<string, string> — Headers added to every response, errors included. Names are matched case-insensitively with the CORS and handler headers; the handler's value wins. Checked at startup: a value that is not a string, or a name or value Node refuses (a line break, a space in a name), throws an error naming the header
logger AladoServerLogger console { log, error }, sync or async. Internal errors are always logged; a logger that fails reports to console.error. See Logging
verbose boolean false Also log errors caused by the client (4xx)
ssl SecureContextOptions — TLS/HTTPS options
serverOptions http.ServerOptions — Passed to Node's http.createServer / https.createServer (timeouts, header limits, ...)
maxBodySizeBytes number 1048576 (1 MB) Larger request bodies get 413 Content Too Large and the connection is closed. 0 removes the limit. A route can set its own (maxBodySizeBytes in its options), which replaces this one for that route. Bodies read as text and multipart text fields are also refused past about 512 MB, the longest string V8 can build
trustProxy boolean | number false Source of request.ip: false the socket address, true the left-most X-Forwarded-For address, a number how many proxies to trust. request.ip is always an IP address: forwarded entries that are not one are never used
caseSensitiveRoutes boolean true Whether /Admin and /admin are different paths. A trailing slash and repeated slashes are always ignored
parsing ParsingOptions { maxFilesPerField: 1, maxParts: 1000, repeatedFiles: 'last' } maxFilesPerField: files one multipart field name may carry (default 1: a second one gets 400; 0 for no limit). maxParts: parts one multipart body may hold, fields and files together (default 1000: more gets 400; 0 for no limit). repeatedFiles: when more are allowed, 'last' gives the last file, 'array' all of them. See File Uploads
handlerTimeoutMs number 60000 (60 s) How long a handler may take to answer, in milliseconds; a slower one gets 503 Service Unavailable and the connection is closed. 0 removes the limit. See Application Setup
bodyIdleTimeoutMs number 30000 (30 s) How long reading a request body may wait for its next part, in milliseconds; a body that stops arriving gets 408 and the connection is closed. 0 removes the limit
securityHeaders boolean | Record<string, string | false> none true adds HSTS, Referrer-Policy, X-Frame-Options and CSP frame-ancestors to every response; an object changes that set (false leaves a header out). headers and handlers win over it
hooks Hooks none Functions run for every request at fixed points: onRequest, beforeHandler, beforeSend, onFinished, each one function or a list. See Hooks
rateLimit RateLimit none { requests, windowMs, perEndpoint? }: requests one IP address may make per window, for the whole API or per endpoint; over it 429 with Retry-After. A route sets its own with @rateLimit (false for none). See Rate Limiting

Request

The object received in every route handler:

Property Type Description
request IncomingMessage Raw Node.js request
ip string Client IP address (see trustProxy)
method string HTTP method
url string Request URL
headers IncomingHttpHeaders Request headers
path Record<string, any> Path parameters declared in the path DTO, URL-decoded
query Record<string, any> Query parameters; a repeated key or key[] gives an array of every value, in order
body Record<string, any> Request body (POST/PUT/PATCH/QUERY, when the route declares something that reads it); missing when the request has no body
rawBody string Unparsed body string (multipart: built when first read); set only when the body is read (rawBody: true for routes without body fields)
rawBodyBytes Buffer The exact bytes of a body with a binary Content-Type (application/octet-stream, image/*, ...)
files Record<string, UploadedFile> Uploaded files: { stream, size, mimetype, filename } (mimetype lower-cased without parameters, filename decoded as UTF-8) (an array per repeated field with parsing.maxFilesPerField above 1 and repeatedFiles: 'array')
auth Record<string, any> Auth handler output
route RouteInfo { method, path } of the route that answers, path as registered (/users/:id); unset where no route matched (3.12.0)
state Record<string, any> Empty for every request and never read by Alado: for hooks and the handler to pass values on (3.12.0)

Hooks

serverOptions.hooks; a route takes the same without onRequest (RouteHooks, in @hooks or Context.hooks). Each property is one function or a list. See Hooks.

Property Type Description
onRequest (request) => void | Response | Promise<...> Before routing; returning a response answers the request
beforeHandler (request) => void | Response | Promise<...> After authentication, access control and validation; returning a response answers the request and the handler is not called
beforeSend (request, response) => void | Response | Promise<...> Before a response is written, errors included; response is a copy that may be changed, or another one returned
onFinished (request, info: FinishedInfo) => void | Promise<unknown> After the response was written or the connection ended; not awaited

FinishedInfo:

Property Type Description
statusCode number The status that was sent; 0 when the connection ended before a status line went out
durationMs number From the moment the request reached Alado to the end of the response
aborted boolean true when the response was not written to its end (the client left)

Response<T>

The object your handler must return:

Property Type Required Description
statusCode number required HTTP status code
headers Record<string, string> — Response headers. They win over server and CORS headers of the same name, whatever the letter case; each header is sent once
body T — Response body — object, string, Buffer, or a readable stream

An object body is sent as JSON. A string or Buffer is sent as it is, also under a JSON Content-Type (an already serialised document).

AladoConfigurationError

Thrown while the application is set up, never for a request. Exported from 'alado'.

Property Type Description
name string 'AladoConfigurationError'
code string 'ROUTE_ALREADY_REGISTERED', 'ROUTE_RESERVED_FOR_OPEN_API', 'HEADER_DECLARED_TWICE', 'BODY_NOT_ALLOWED', 'INVALID_PATH_PARAMETER' or 'DECORATOR_REPEATED'
message string Names the route (and, for a conflict, the route registered first or the header)

HttpError

An error meant for the client: new HttpError(statusCode, message). Thrown from a handler it is answered with that status and message, like a plain { statusCode, message } object. Any other Error is answered with a generic 500. Exported from 'alado'.

Property Type Description
name string 'HttpError'
statusCode number The status of the response (400 to 599)
message string Sent to the client as { "message": ... }