HttpServer

class HttpServer extends Router - the server does not start automatically.

Constructor

new HttpServer(options?: HttpServerOptions)

  • options.trustProxy (default false) - resolve req.ip from x-forwarded-for (first hop). Only behind a trusted proxy.

Default CORS

HttpServer initializes CORS rules to:

  • allowOrigin: "*"
  • allowMethods: "GET, POST, PUT, PATCH, DELETE, OPTIONS"
  • allowHeaders: "*"
  • maxAge: "86400"
  • allowCredentials: false

Override with .cors(). See CORS and Router.

Methods

Everything from Router, plus:

  • listen(port: number): void - Deno.serve({ port }, this.fetch)
  • fetch(request: Request, info?): Promise<Response> - standard fetch handler
  • notFound(handler: RequestListener): this - custom 404
  • onError(handler: ErrorListener): this - global error handler

Default error bodies

  • 404: { success: false, error: "404 Not Found." }
  • 500: { success: false, error: "500 Internal Server Error." }
  • 400 (validation): { success: false, error: "400 Bad Request.", details }