KScript docs
EN

Routes and APIs

Basic routing

Route files

A route file imports KScript.Http and holds a Routes { } block (<KScript.Routes> in XML), the same way a Window { } block makes a desktop app. Its imports go at the top of the file, above the block. The simplest route answers with a body of its own:

<!-- routes.kscript -->
<KScript.Routes>
  <Get path="/ping">
    <return value="'pong'" />
  </Get>
</KScript.Routes>
// routes.kscript
import KScript.Http

Routes {
  Get(path: /ping) {
    return 'pong'
  }
}

kscript serve loads route files from these places in the site folder:

File Routes
routes/web.kscript, or routes.kscript at the top (or .ks.xml) Web routes, mounted at /.
routes/api.kscript, or api.kscript at the top (or .ks.xml) API routes, mounted under /api. Their errors and missing routes are answered with JSON.

If you would like to load other files, list them in http.routes in kscript.json (see Settings).

You may generate a route and its controller with kscript make (see Generating files):

CommandWhat it does
kscript make route usersA route and its controller.
kscript make route "people/{id}" --apiAn API route with a path parameter.
kscript make route people --resourceResource routes and a controller with their functions.

kscript make controller, request, middleware and view make the rest.

Methods

There is a route for each HTTP method: Get, Post, Put, Patch, Delete and Options. Get answers HEAD too. Any answers every method, and Route(methods: [get, post]) answers the ones you list:

<!-- routes.kscript -->
<KScript.Routes>
  <import path="controllers/people.kscript" as="people" />

  <Get path="/people" to="people.index" />
  <Post path="/people" to="people.store" />
  <Route path="/search" methods="get, post" to="people.index" />
</KScript.Routes>
// routes.kscript
import KScript.Http
import "controllers/people.kscript" as people

Routes {
  Get(path: /people, to: @people.index)
  Post(path: /people, to: @people.store)
  Route(path: /search, methods: [get, post], to: @people.index)
}

Routes are tried in the order they are written, and the first match wins. A path that matches but has no route for the method gets 405 Method Not Allowed with an Allow header. An OPTIONS request gets 204 with Allow, which is how CORS preflights are answered (see cors).

How a route answers

Each route has a path and one way to answer it:

  • to: names the controller, a function of an imported script (@people.show) or an fn in the route file itself (@show). In XML it is written to="people.show".
  • view: renders a view, with the path parameters as its variables.
  • redirect: sends the browser somewhere else, with 302 unless you give a status:.
  • A { } block is the route's own body, like an fn.
<!-- routes.kscript -->
<KScript.Routes>
  <import path="controllers/people.kscript" as="people" />

  <Get path="/" view="home" />
  <Get path="/people/{id}" to="people.page" />
  <Get path="/team" redirect="/people" status="301" />
</KScript.Routes>
// routes.kscript
import KScript.Http
import "controllers/people.kscript" as people

Routes {
  Get(path: /, view: 'home')
  Get(path: /people/{id}, to: @people.page)
  Get(path: /team, redirect: '/people', status: 301)
}

The route file runs on every request, before the route is matched. So it may use if to add routes only in some cases, and its imports are new for each request, just like a page's.

Edit this page on GitHub