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.
$thread[html]
<h1>Hello $request[query.name]</h1>
<p>You asked for $request[path] with $request[method].</p>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.
$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>]GET / → 200 <h1>Hi Sam <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.
$print[$escapeHtml[<b>Tom & Jerry</b>]]<b>Tom & Jerry</b>
$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.
$let[bold;<b>trusted</b>]
$thread[html]
<p>Safe: $get[bold]</p>
<p>As HTML: $rawHtml[$get[bold]]</p>
$endthreadGET / → 200 <p>Safe: <b>trusted</b></p> <p>As HTML: <b>trusted</b></p>
$statuswebNaja page
$status[code]
In a webNaja page: the answer's status code, like $status[404].
$status[404]
$thread[html]
<h1>No such player</h1>
$endthreadGET / → 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].
$setHeader[Content-Type;text/plain]
$print[Plain text, not a page]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).
$if[$request[cookie.seen] == "yes"]
$print[Welcome back]
$else
$setCookie[seen;yes;3600]
$print[First visit]
$endifGET / → 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.
$paths[$list[maths;text]]
$page[$request[path.group]]
$heading[All about $request[path.group]]
$endpage
$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.
$if[$request[query.page] == "old"]
$redirect[/new]
$endif
$print[This is the page]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]].
$sendJson[$table[name;Sam;level;$request[json.level] + 1]]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.
$if[$request[query.name] != nil]
$setSession[name;$request[query.name]]
$endif
$print[Signed in as $session[name]]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.
$setSession[cart;$list[apple;pear]]
$print[$count[$session[cart]] things in the cart]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).
$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]]
$endifGET /?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.
$rateLimit[2;60]
$print[Here is the page]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.