dart_server 1.0.0
dart_server: ^1.0.0 copied to clipboard
A lightweight, Express.js-like HTTP server framework for Dart. Build REST APIs with routing, middleware, and JSON handling using zero external dependencies.
Changelog #
1.0.0 #
Initial release.
Modular architecture (NestJS-style, optional) #
Module— groupsprovidersandcontrollers, withimports/exportsand per-module provider encapsulation (plusisGlobal).Provider— dependency injection withsingleton,transientandvaluescopes, resolved viaInjector.get<T>(); missing providers and circular dependencies fail fast withDiError.Controller— class-based route grouping under abasePathviaRouteRegistrar, with constructor injection.OnInit— async lifecycle hook awaited during bootstrap in dependency order.DartServerFactory.create(rootModule)— wires the graph and mounts every controller's routes; returns an ordinaryDartServer.- No decorators, reflection or codegen — wiring is plain, analyzable Dart.
- CLI:
dart_server createscaffolds a modular app;make:resource/make:module/make:controller/make:service/make:repository/make:modelgenerate feature modules underlib/modules/<name>/.
Routing & requests #
- Express-style
DartServerwithget/post/put/delete/patch/head/options/all. - Path parameters (
/users/:id) and a trailing wildcard (/files/*); a*in any non-final segment is rejected at registration. - Automatic
HEAD→GETfallback with the response body stripped. Requestwithpath,method,headers,query,params,bodyBytes,body,context, and a lazy, cachedjson().- Raw
bodyBytesalways preserved (binary-safe);bodydecodes UTF-8 with malformed bytes replaced rather than thrown;json()re-throws on every call for invalid JSON instead of cachingnull. - Configurable
maxBodyBytes(default 1 MiB) — oversized bodies are rejected with413before any handler runs. - Malformed percent-encoding in a path segment falls back to the raw value
instead of producing a
500.
Responses #
Response.json,text,html,status,bytes,redirect, fluent.header().HEADresponses send theContent-Lengthbut no body;204/304/1xxresponses send neither a body nor aContent-Length.
Middleware & errors #
- Global middleware with
next()chaining and per-requestcontext. - Handler errors are converted to responses inside the chain, so middleware
(and
logger/cors) observe error responses too. HttpErrorwith status-mapped constructors; customizableonError.
CLI (dart_server) #
dart_server create <name>— scaffolds a ready-to-run app (entry point, app wiring, routes, controllers/models/repositories layout) and runspub get.dart_server dev— runs withDART_SERVER_ENV=developmentand auto-restarts on.dartchanges underlib//bin/;dart_server prodruns in production.dart_server make:model|controller|repository|middleware|service|resource— code generators with name normalization (snake/camel/Pascal) and--force.- Installable via
dart pub global activate dart_server; zero dependencies (hand-rolled argument parsing).
Dev tools #
app.useDevTools()— an in-process development dashboard at/__devthat tracks recent requests (method, path, status, timing, headers, request/ response bodies), aggregate stats, the live route table and server info, with a JSON snapshot at/__dev/api. Self-contained (no external assets).- Development-only: disabled when
DART_SERVER_ENV/DART_ENV/ENVis production-like; configurable mount path, buffer size and body capture.
Bundled middleware #
logger()— logs successful and errored requests.cors()— secure by default:credentials: truewith a wildcard origin is refused; use theoriginsallow-list for credentialed cross-origin access. Pre-flights are detected byAccess-Control-Request-Methodso explicitapp.options(...)routes still run.serveStatic()— rejects..and symlink path-traversal (real target re-checked against the root).
Other #
- Zero external runtime dependencies (built on
dart:io+dart:convert). listen(..., quiet: true)suppresses the startup banner.