router
Routing
Routes handle how a WebApp responds to client requests. All WebApps have a unique segment in the URL where routing information is kept. WebApp routes differ from application routes since WebApp routes are local and should be used to keep state for a single WebApp. The router object provides methods to respond to the following request methods:
- GET
- PUT
- POST
- DELETE
- PATCH (@since 2023.02.1)
A route definition has the following structure:
Basic examples:
Middleware
A middleware function is a function that have access to the request object (req
), response object (res
) and a function (next
) triggering the next function in the request-response chain. The next function will either be another middleware or the actual route function. Middleware functions are executed every time the app receives a request matching a route.
Middleware functions are registered using the router.use
-method:
Requests may be terminated and altered within a middleware. Keep in mind that the next()
-function must be called in order to pass over control to the next function in the stack.
Multiple middleware functions may be registered and they will be executed in the order they are registered, i.e. top-down.
Middleware can be a very useful tool when managing persissons in your application to ensure all endpoints are secured. A basic example is available on GitHub.
Route parameters
Route parameters are used to capture a value in the URL. Below is an example of a path that will capture id
on the /user
route.
Router methods
router.getUrl(path [, queryStringParameters])
Returns a URL given a path. Provide queryStringParameters
as an object to include query string parameters. The URL will match configured paths in index.js.
router.getStandaloneUrl(path [, queryStringParameters])
Returns a URL given a path. This type or URL is used to execute a route standalone, i.e. it will not be part of page rendering. This is very useful when you only want to target your route without any side effects. The SDK documentation contains example of client-side usage.