Skip to the page
Najova

Docs

/

Guides

Servers and APIs

A webNaja file can answer requests as well as show pages: read what was sent, answer with JSON, remember visitors and run code when a button is clicked.

What a request brings

Every .wnaj file runs on the server each time someone asks for it. $request says what they sent: the address, the method, and values from the address, a form, cookies or headers. A file with no $page answers with what it prints.

index.wnaj
$-- No $page: what $print says is the whole answer.
$let[name;$request[query.name]]
$if[$get[name] == nil]
  $set[name;there]
$endif
$print[Hello $get[name]! You asked for $request[path] with $request[method].]

› naja serve, then open /?name=Sam, /

GET /?name=Sam  →  200
Hello Sam! You asked for / with GET.

GET /  →  200
Hello there! You asked for / with GET.

The parts are method, path, body, ip, json.<name>, query.<name>, form.<name>, header.<name> and cookie.<name>. One that wasn't sent is nil.

Answering with JSON

$sendJson answers with a value as JSON, which is how an API talks to apps and other sites. A second value sets the status, like 400 when something is missing. Put API files in an api/ folder and they get addresses like /api/score.

api/score.wnaj
$-- An API: JSON in, JSON out. Its address is /api/score.
$let[points;$request[json.points]]
$if[$get[points] == nil]
  $sendJson[$table[error;Send points as a number.];400]
$else
  $sendJson[$table[points;$get[points];stars;$floor[$get[points] / 2]]]
$endif

› naja serve, then open /api/score, /api/score

POST /api/score  →  200
{"points":7,"stars":3}

POST /api/score  →  400
{"error":"Send points as a number."}

$status and $setHeader set the status and headers of any answer, and $redirect sends the visitor to another address.

Forms

A $form sends its fields to the same page. When the method is POST, read each field with $request[form.<name>], then redirect, so pressing Refresh doesn't send it twice.

index.wnaj
$if[$request[method] == "POST"]
  $setSession[note;$request[form.message]]
  $redirect[/thanks]
$endif

$page[Contact]
  $form[method=post]
    $input[message;Message;type=textarea;required=yes]
    $button[Send;look=primary]
  $endform
$endpage

› naja serve, then open /, /thanks

POST /  →  302
Set-Cookie: najova_session=…; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800
Location: /thanks
Moved to /thanks

GET /thanks  →  200
<main class="nj-page" id="page">
<p class="nj-paragraph">We got your message: Two loaves please</p>
</main>

Remembering visitors

$setSession keeps a value for one visitor between pages, and $session reads it back. It's how sign-in works: the visitor's browser only holds a long random number, and the values stay on the server. $endSession forgets them all, for 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 /  →  200
Signed in as Sam

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

$setCookie keeps a small value in the browser itself instead. Cookies are sent with HttpOnly and SameSite=Lax, so scripts on other sites can't read them.

Buttons that run on the server

Give a button a name= and its click runs $on[name.Clicked] on the server. There, the page's named parts are values you can change, and only what changed is sent back: the page doesn't reload, and you write no JavaScript.

index.wnaj
$page[Bakery]
  $button[How many loaves are left?;name=ask]
  $paragraph[ ;name=answer]
$endpage

$-- Runs on the server: only the paragraph changes.
$on[ask.Clicked]
  $setProperty[$get[answer];text;12 loaves left today.]
$endon

› naja serve, then open /, /

GET /  →  200
<main class="nj-page" id="page">
<button type="button" class="nj-button" data-nj="ask">How many loaves are left?</button>
<p class="nj-paragraph" data-nj="answer"></p>
</main>

POST /  →  200
{"changes":[{"part":"answer","what":"text","value":"12 loaves left today."}]}

Keeping a server safe

$rateLimit lets each visitor load a page only so many times in a while. After that they're told to wait, with the status 429.

index.wnaj
$rateLimit[2;60]
$print[Here is the price list.]

› naja serve, then open /, /, /

GET /  →  200
Here is the price list.

GET /  →  200
Here is the price list.

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

Text from a request is made safe before it goes in a page, so a visitor can't add their own HTML or scripts to it. Values you print with $thread[html] are made safe the same way.

Asking other servers

$fetch asks another server for something and gives back its status, text and headers. With $fromJson, an API's answer becomes a table.

index.wnaj
$let[answer;$fetch[https://api.example.com/weather?city=Perth]]
$if[$get[answer.ok]]
  $let[weather;$fromJson[$get[answer.text]]]
  $print[It's $get[weather.temp] degrees in Perth.]
$else
  $status[502]
  $print[The weather service didn't answer.]
$endif

Shown without running, so the docs build doesn't need the internet.

Running it

naja serve runs the server on your computer while you work on it. Pages that don't need a server can be made into plain files with naja build --web and put on any host. Running naja serve for a real site, with HTTPS and many visitors at once, is a Canary goal on the roadmap.