Najova

DocsBuilt-in functions

Websites and APIs

Answer visitors in a webNaja page: a .wnaj file, or one that starts with $use[webnaja]. naja serve runs the site; each visit runs the page.

$requestwebNaja page

$request or $request[part]

In a webNaja page: the request being answered. Parts: method, path, body, ip, query.<name>, form.<name>, header.<name>, cookie.<name>; with nothing, a table of all of them.

index.wnaj
$thread[html]
<h1>Hello $request[query.name]</h1>
<p>You asked for $request[path] with $request[method].</p>
naja serve, then open /?name=Sam
GET /?name=Sam  →  200
<h1>Hello Sam</h1>
<p>You asked for / with GET.</p>

$threadwebNaja page

$thread[html] ... $endthread

Writes another language straight into the file: the lines up to $endthread are HTML, with Najova calls inside them ($get[name] and so on; their text is made safe for HTML). On the first line it threads the whole file.

index.wnaj
$let[name;Sam <3]
$thread[html]
<h1>Hi $get[name]</h1>
<p>Text from values is made safe.</p>
$endthread
$print[<p>Back in Najova.</p>]
naja serve, then open /
GET /  →  200
<h1>Hi Sam &lt;3</h1>
<p>Text from values is made safe.</p>
<p>Back in Najova.</p>

$escapeHtmlwebNaja page

$escapeHtml[text]

The text with < > & " and ' written so a browser shows them instead of reading them as HTML.

escapeHtml.naja
$print[$escapeHtml[<b>Tom & Jerry</b>]]
najova run escapeHtml.naja
&lt;b&gt;Tom &amp; Jerry&lt;/b&gt;

$rawHtmlwebNaja page

$rawHtml[text]

Inside $thread[html]: puts the text in the page as HTML, without making it safe first. Only for HTML you trust.

index.wnaj
$let[bold;<b>trusted</b>]
$thread[html]
<p>Safe: $get[bold]</p>
<p>As HTML: $rawHtml[$get[bold]]</p>
$endthread
naja serve, then open /
GET /  →  200
<p>Safe: &lt;b&gt;trusted&lt;/b&gt;</p>
<p>As HTML: <b>trusted</b></p>

$statuswebNaja page

$status[code]

In a webNaja page: the answer's status code, like $status[404].

index.wnaj
$status[404]
$thread[html]
<h1>No such player</h1>
$endthread
naja serve, then open /
GET /  →  404
<h1>No such player</h1>

$setHeaderwebNaja page

$setHeader[name;value]

In a webNaja page: a header of the answer, like $setHeader[Content-Type;application/json].

index.wnaj
$setHeader[Content-Type;text/plain]
$print[Plain text, not a page]
naja serve, then open /
GET /  →  200
Plain text, not a page

$setCookiewebNaja page

$setCookie[name;value] or $setCookie[name;value;seconds]

In a webNaja page: keeps a value in the visitor's browser (for the seconds given, or until it closes).

index.wnaj
$if[$request[cookie.seen] == "yes"]
  $print[Welcome back]
$else
  $setCookie[seen;yes;3600]
  $print[First visit]
$endif
naja serve, then open /, /
GET /  →  200
Set-Cookie: seen=yes; Path=/; HttpOnly; SameSite=Lax; Max-Age=3600
First visit

GET /  →  200
Welcome back

$pathswebNaja page

$paths[list]

In a page whose address has a [value] part (like [group].wnaj): the values it has, like $paths[$list[maths;text]]. naja build --web makes one page for each; naja serve answers 404 for any other. With more than one [value], each is a table: $table[ext;discord;pkg;xp]. Put it near the top.

[group].wnajnot run here
$paths[$list[maths;text]]
$page[$request[path.group]]
  $heading[All about $request[path.group]]
$endpage
Saved as reference/[group].wnaj: naja serve answers /reference/maths and /reference/text (any other value gets the 404 page), and naja build --web makes reference/maths/index.html and reference/text/index.html.

$redirectwebNaja page

$redirect[path]

In a webNaja page: sends the visitor to another page instead, like $redirect[/login]. The rest of the page does not run.

index.wnaj
$if[$request[query.page] == "old"]
  $redirect[/new]
$endif
$print[This is the page]
naja serve, then open /?page=old, /
GET /?page=old  →  302
Location: /new
Moved to /new

GET /  →  200
This is the page

$sendJsonwebNaja page

$sendJson[value] or $sendJson[value;status]

In a webNaja page: answers with the value as JSON instead of a page (for APIs), like $sendJson[$get[user]].

level.wnaj
$sendJson[$table[name;Sam;level;$request[json.level] + 1]]
naja serve, then open /level
POST /level  →  200
{"name":"Sam","level":5}

$sessionwebNaja page

$session or $session[name]

In a webNaja page: a value kept for this visitor between pages (nothing if not set); with nothing, a table of all of them.

index.wnaj
$if[$request[query.name] != nil]
  $setSession[name;$request[query.name]]
$endif
$print[Signed in as $session[name]]
naja serve, then open /?name=Sam, /
GET /?name=Sam  →  200
Set-Cookie: najova_session=…; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800
Signed in as Sam

GET /  →  200
Signed in as Sam

$setSessionwebNaja page

$setSession[name;value]

In a webNaja page: keeps a value for this visitor between pages, like $setSession[user;$get[name]]; an empty value removes it.

index.wnaj
$setSession[cart;$list[apple;pear]]
$print[$count[$session[cart]] things in the cart]
naja serve, then open /
GET /  →  200
Set-Cookie: najova_session=…; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800
2 things in the cart

$endSessionwebNaja page

$endSession

In a webNaja page: forgets everything kept for this visitor (signing out).

index.wnaj
$if[$request[query.name] != nil]
  $setSession[name;$request[query.name]]
$endif
$if[$request[query.out] == "yes"]
  $endSession
$endif
$if[$session[name] == nil]
  $print[Not signed in]
$else
  $print[Signed in as $session[name]]
$endif
naja serve, then open /?name=Sam, /?out=yes, /
GET /?name=Sam  →  200
Set-Cookie: najova_session=…; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800
Signed in as Sam

GET /?out=yes  →  200
Set-Cookie: najova_session=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0
Not signed in

GET /  →  200
Not signed in

$rateLimitwebNaja page

$rateLimit[times;seconds]

In a webNaja page: lets each visitor load this page at most so many times in so many seconds; after that it answers 429 Too many requests.

index.wnaj
$rateLimit[2;60]
$print[Here is the page]
naja serve, then open /, /, /
GET /  →  200
Here is the page

GET /  →  200
Here is the page

GET /  →  429
Retry-After: 60
Too many requests: try again in 60 seconds.