Docs
Guide
Context

Context

dobs is built on top of a base server that implements middleware using Node’s http module. This means the request and response instances passed to handlers support all features provided by Node http, along with additional custom utilities.

For details on how the context is implemented, refer to the source code.


  • Response
SectionDescription
req.pathnameURL pathname
req.hrefFull URL href
req.searchURLSearchParams instance
req.searchStringRaw search string
req.queryParsed query object
req.URLURL instance
req.rawBodyRaw request body string
  • Request
SectionDescription
res.statusSet HTTP status code
res.messageSet HTTP status message
res.typeSet Content-Type
res.redirectRedirect to URL
res.setSet headers
res.getGet header
res.removeRemove header
res.hasCheck header existence
res.isWritableCheck writable state
res.sendAuto-detect and send data
res.textSend plain text
res.jsonSend JSON
res.htmlSend HTML
res.sendFileSend file

#Request

#req.pathname

Type: string Provides the URL pathname.

Typescript
console.log(req.pathname);
// "/users/123"

#req.href

Type: string Provides the full URL href.

Typescript
console.log(req.href);
// "http://localhost:3000/users/123?active=true"

#req.search

Type: URLSearchParams Provides the URL search parameters.

Typescript
const active = req.search.get("active");
console.log(active);
// "true"

#req.searchString

Type: string Provides the raw search string (without "?").

Typescript
console.log(req.searchString);
// "active=true&page=4"

#req.query

Type: object Returns the search parameters as a plain object.

Typescript
console.log(req.query);
// { active: "true", page: "4" }

#req.URL

Type: URL Returns a URL instance built from the incoming request.

Typescript
console.log(req.URL.hostname); // "localhost"
console.log(req.URL.pathname); // "/users/123"

#req.rawBody

Type: string Provides the raw request body as a string.

Typescript
server.post("/submit", (req, res) => {
  console.log(req.rawBody);
  // "{ \"message\": \"hello\" }"
  res.send("ok");
});

If you need accurate and structured body parsing (e.g., JSON, form data), use an external middleware such as body-parser.

#Response

The Response object extends Node’s native ServerResponse and provides additional helper methods commonly used in web frameworks. These utilities help you set headers, send data in various formats, and manage response states more easily.

#res.status

Type: (statusCode: number) => this Sets the HTTP status code.

Typescript
res.status(404).send("Not found");

#res.message

Type: (message: string) => this Sets the HTTP status message.

Typescript
res.status(400).message("Bad Request").send();

#res.type

Type: (type: string) => this Sets the Content-Type header. Supports file extensions, short names, and full MIME types.

Typescript
res.type(".json");
res.type("json");
res.type("application/json");

#res.redirect

Type: (url: string) => this Performs a 302 redirect to the given URL.

Typescript
res.redirect("https://example.com");

#res.set

Type: (field: string | Record<string, any>, value?: any) => this Sets one or multiple response headers.

Typescript
res.set("foo", "bar");
 
res.set({
  "x-powered-by": "dobs",
  "cache-control": "no-store",
});

#res.get

Type: (field: string) => any Gets the value of a response header.

Typescript
console.log(res.get("content-type"));

#res.remove

Type: (field: string) => this Removes a header.

Typescript
res.remove("x-powered-by");

#res.has

Type: (field: string) => boolean Checks if a header is already set.

Typescript
if (!res.has("content-type")) {
  res.type("text/plain");
}

#res.isWritable

Type: () => boolean Checks whether the response is still writable (not finished or closed).

Typescript
if (!res.isWritable()) return;
res.send("ok");

#Data Sending Methods

#res.send

Type: (data: any) => void Automatically detects the data type and sends it with the appropriate Content-Type.

Supports: string, Buffer, object, number, etc.

Typescript
res.send("hello world"); // text
res.send({ ok: true }); // JSON
res.send(Buffer.from("hi")); // binary

If you want to manually control output, use res.end() instead.


#res.text

Type: (data: any) => void Sends raw text with Content-Type: text/plain.

Typescript
res.text("plain text response");

#res.json

Type: (data: any) => void Sends JSON with Content-Type: application/json.

Typescript
res.json({ user: "yo", id: 1 });

#res.html

Type: (data: string) => void Sends HTML with Content-Type: text/html.

Typescript
res.html("<h1>Hello</h1>");

#res.sendFile

Type: (file: string) => void Sends a file from disk.

Typescript
res.sendFile("./public/index.html");