Skip to content

API Reference

Complete reference for Daho's public API.

Daho

The main application class. Register routes and middleware, then call listen() to start serving.

dart
final app = Daho(config: const DahoConfig());

Constructor

dart
Daho({DahoConfig config = const DahoConfig()})

Properties

PropertyTypeDescription
configDahoConfigGlobal configuration

Methods

MethodDescription
get(path, handler, {use:})Register a GET route
post(path, handler, {use:})Register a POST route
put(path, handler, {use:})Register a PUT route
delete(path, handler, {use:})Register a DELETE route
patch(path, handler, {use:})Register a PATCH route
use(middleware)Register global middleware
group(prefix)Create a route group
serveStatic(virtualPath, localDir)Serve static files via H2O
fastPath(path, body, {contentType:})Register a native fast path
listen(port, {routes:, onStart:, onShutdown:})Start the server

listen()

dart
Future<void> listen(
  int port, {
  required AppBuilder routes,
  Function? onStart,
  FutureOr<void> Function()? onShutdown,
})

Spawns one worker Isolate per CPU core (configurable via DahoConfig.concurrency). Each worker rebuilds the route table by calling routes.


DahoGroup

A group of routes sharing a common path prefix and middleware.

dart
final api = app.group('/api');
api.use(authMiddleware);
api.get('/users', listUsers);
api.post('/users', createUser);

Methods

MethodDescription
use(middleware)Register middleware scoped to this group
get(path, handler, {use:})Register a GET route in the group
post(path, handler, {use:})Register a POST route in the group
put(path, handler, {use:})Register a PUT route in the group
delete(path, handler, {use:})Register a DELETE route in the group
patch(path, handler, {use:})Register a PATCH route in the group

DahoConfig

Immutable, Isolate-safe configuration.

dart
const DahoConfig({
  int bodyLimit = 4 * 1024 * 1024,
  int? concurrency,
  Duration requestTimeout = Duration.zero,
  Duration idleTimeout = Duration.zero,
  Duration shutdownGracePeriod = const Duration(seconds: 5),
  ErrorHandler errorHandler = defaultErrorHandler,
  NotFoundHandler notFoundHandler = defaultNotFoundHandler,
  bool trustProxy = false,
  bool disableStartupMessage = false,
})

See Configuration for detailed descriptions.


DahoRequest

The incoming HTTP request.

Properties

PropertyTypeDescription
methodStringHTTP method
pathStringRequest path
queryMap<String, String>Query parameters
paramsMap<String, String>Path parameters
ipStringClient IP
headersMap<String, String>Request headers (lowercase keys)
bodydynamicParsed body (lazy)
filesMap<String, UploadedFile>Uploaded files (lazy)
cookiesMap<String, String>Parsed cookies (lazy)

Methods

MethodReturnsDescription
header(name)String?Case-insensitive header lookup

DahoResponse

The response builder. All methods return this for chaining.

Sending Data

MethodDescription
send(text)Send plain text
json(data)Send JSON
bytes(raw, {contentType:})Send raw bytes

Status Helpers

MethodStatus
ok(data)200
badRequest(data)400
unauthorized(data)401
forbidden(data)403
notFound(data)404
internalServerError(data)500

Redirects

MethodStatus
movedPermanently(location, data)301
found(location, data)302
seeOther(location, data)303
notModified()304

Headers & Status

MethodDescription
status(code)Set HTTP status code
header(key, value)Set a response header

Cookies

MethodDescription
cookie(name, value, {maxAge:, expires:, path:, domain:, secure:, httpOnly:, sameSite:})Set a cookie
clearCookie(name, {path:})Clear a cookie

UploadedFile

A file from a multipart/form-data upload.

Properties

PropertyTypeDescription
filenameStringOriginal filename
contentTypeStringMIME type
bytesList<int>Raw file contents

Methods

MethodDescription
save(path)Write to disk (sync)
saveAsync(path)Write to disk (async)

Middlewares

Built-in middleware factory.

MethodDescription
Middlewares.logger({out:})Access logging
Middlewares.cors({origin:, methods:, headers:, credentials:, maxAge:})CORS + preflight
Middlewares.secureHeaders()Security headers
Middlewares.compress({minLength:})Gzip compression

Typedefs

dart
/// Route setup function (must be top-level)
typedef AppBuilder = void Function(Daho app);

/// Middleware function
typedef Middleware = FutureOr<void> Function(
  DahoRequest req,
  DahoResponse res,
  NextFunction next,
);

/// Called by middleware to continue the chain
typedef NextFunction = Future<void> Function();

/// Terminal route handler
typedef RouteHandler = FutureOr<DahoResponse> Function(
  DahoRequest req,
  DahoResponse res,
);

/// Error handler
typedef ErrorHandler = FutureOr<void> Function(
  DahoRequest req,
  DahoResponse res,
  Object error,
  StackTrace stackTrace,
);

/// Not-found handler
typedef NotFoundHandler = FutureOr<void> Function(
  DahoRequest req,
  DahoResponse res,
);

Released under the MIT License.