JavaScript Endpoints

Learn how to use JavaScript to create custom logic for your views and endpoints.

Stacklane uses a JavaScript subset for server-side logic. This subset is designed for speed, security, and compile time validation.

In many cases JavaScript endpoints are used in conjunction with JavaScript Suppliers. Suppliers allow for code reuse between multiple endpoints, creating simpler endpoint code.

Scripts are executed on the server-side only, and their last emitted value becomes the response value. Scripts never produce HTML, which is better suited for Mustache files.

Routing

The file name is used to determine both the endpoint path and the HTTP verb it responds to. There are several ways of defining JavaScript endpoints, and it's a matter of preference which to choose. There is no functional difference. Note that in all cases JavaScript endpoints do not end in a trailing "/".

Verb Name Only

Place an uppercase verb name in a directory. GET.js, POST.js, PUT.js, DELETE.js.

For example /hello/POST.js responds to requests for POST /hello.

Or for a dynamic path such as /product/{product}/GET.js, a corresponding request could be /product/1234.

Index / Directory

JavaScript endpoints by default do not respond to directory requests ending in "/". In certain cases directory requests are useful however, especially if the JS endpoint is redirecting to other endpoints. In this case use 🖥index.js, such as /here/🖥index.js to have the endpoint respond at GET /here/.

Emoji and Name

Verbs are represented by a specific an emoji prefix.

  • 🖥 - GET
  • 📮 - POST
  • ❌ - DELETE

The rest of the name after verb prefix becomes the endpoint name. For example a file name of /account/{account}/🖥settings.js responds to requests for GET /account/1234/settings

Redirects

Redirects

  • Internal redirects - Output a string prefixed with "/" to perform an absolute redirect to another endpoint within your app. For more readable code and additional options, use the Redirect builder instead.
  • External redirects - Output a string prefixed with "http" to perform an external redirect.

Rerouting

Similar to an internal redirect, but no extra client request is made. A few conditions must exist for this to occur.

  • It must be a POST/PUT/DELETE request.
  • "X-Requested-With" header must exist and equal "XMLHttpRequest" (denoting an AJAX request).
  • The script must emit an absolute path for the app, similar to a regular internal redirect.

Given a POST.js code called via AJAX and returning '/show/all', the server would internally route and return the result of executing GET /show/all, instead of requiring the client to round trip as it would with a normal internal redirect.

JSON Output

Output an object literal from the script to create a JSON response. For example:


({
  hello: 'world'
});
            
Result:

{
  "hello": "world"
}
            
Stream aware model methods may also be used:

/* Code to assign LoadedList variable */

({
  list: LoadedList.name,
  tasks: LoadedList(()=>Task.all().map(t=>({name:t.title}}))
});
            
Result:

{
  "list": "hello"
  "tasks": [{"name":"world"}]
}