Skip to content

Response

The @server/response module controls the HTTP response.

import response from "@server/response"

By default, responses use status 200 and content type text/html.

Writes content to the response buffer.

Arguments:

NameTypeDescription
contentanyContent to write. Converted to string.

Returns: void

import response from "@server/response"
response.write(<h1>Hello</h1>)

Sets the status code.

Arguments:

NameTypeDescription
codeintHTTP status code from 100 to 599.

Returns: void

import response from "@server/response"
response.status(404)
response.write("Not found")

Status codes outside 100..599 throw an error.

Sets a response header.

Arguments:

NameTypeDescription
keystringHeader name.
valuestringHeader value.

Returns: void

import response from "@server/response"
response.header("Content-Type", "text/plain")
response.write("Hello")

Sends data as JSON.

Arguments:

NameTypeDescription
dataanyData to JSON encode.

Returns: void

import response from "@server/response"
response.json({
"ok": true,
"message": "Hello from Nubo"
})

This sets the HTTP content type to application/json.

Sets a cookie.

Arguments:

NameTypeDefaultDescription
namestringnoneCookie name.
valuestringnoneCookie value.
maxAgeint?nilOptional cookie max age.
pathstring"/"Cookie path.

Returns: void

import response from "@server/response"
response.setCookie("session", "abc123")

With max age:

response.setCookie("session", "abc123", 3600)

With max age and path:

response.setCookie("session", "abc123", 3600, "/")

Redirects the request to another URL.

Arguments:

NameTypeDescription
urlstringTarget URL.

Returns: void

import response from "@server/response"
response.redirect("/login")

Redirect uses HTTP 302 Found.

Clears the buffered response body.

Returns: void

import response from "@server/response"
response.write("old")
response.flushbuf()
response.write("new")

After the .nubo file finishes, Nubo syncs the response:

  1. Copies headers.
  2. Writes the status code.
  3. Writes the buffered body.

A redirect marks the response as already written.