Develop Routes
Routing is a popular server pattern to clearly define the accepted HTTP requests and how to handle their responses. The following code is an example of creating an app, a very basic route and starting the server.
❐ Copy
Route Methods
A route method is derived from one of the HTTP methods, and is attached to an instance of the class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.
❐ Copy
The following route methods are supported.
- •
- •
- •
- •
- •
- •
- •
- •
- •
To accept any HTTP method, you can use the method. The following code is an example of a route that accepts any HTTP method to the root of the app.
❐ Copy
For any other HTTP method not naturally supported, you can still use the . All naturally supported routes eventually use this method. The following code is an example of a route that accepts the MOVE method to the root of the app.
❐ Copy
Route Paths
A route path is a string that defines the URL pathname for the accepted request. The following code is a very basic example of a route path that accepts the path as is.
❐ Copy
Route paths can also be formed as patterns to represent dynamic variable pathing. The following code is an example of a route path with a named variable called .
❐ Copy
The above example shows how the app accepts the method of the path. This means the following path examples are accepted.
- •
- •
Where as would be , or . The parameter is a variable that can be extracted using the method. Another pattern called a wildcard can be used to match any single path segment.
❐ Copy
The above code is an example of a route path that accepts the GET method to the path. Unlike variable paths, the wildcard matches are nameless and thus denoted by numbers. This means the following path examples are accepted.
- •
- •
Where as would be , or . A more powerful pattern than the is the rest pattern that matches any number of path segments.
❐ Copy
The above code is an example of a route path that accepts the GET method to the path. In this example, any requests pathnames that start with will be accepted. This means the following path examples are accepted (but not limited to).
- •
- •
The wildcard matches any single path segment, while the wildcard matches any number of path segments. The parameter is a variable that can be used to extract the value from the URL.
Route Priority
A route priority is a number that determines the order in which the routes are executed. The higher the number, the higher the priority. The following code is an example of three routes with different priorities.
❐ Copy
The above code is an example of three routes with different priorities. The first route has a priority of , the second route has a priority of , and the third route has a priority of . This means the first route will be executed first, followed by the second route, and finally the third route. The default priority is .
Route Actions
A route action is a function that is executed when the route is matched. The following shows an example of a basic route action. Where the and are the Request, and Response objects, respectively.
❐ Copy
The simplicity of the above code is what made this kind of routing popular, but it comes with a few issues.
- • The actions are defined, but not necessarily used. Consider a serverless environment.
- • No route mapping for bundlers. Cannot do tree shaking or code splitting for example.
- • Cannot be used effectively with a module loader. The action is not a module, and thus cannot be loaded.
Because of the reasons above, the method can also accept a function that returns an import. The following code is an example of a route action that returns a module.
❐ Copy
The above code is an example of a route action that returns a module. The module is lazily loaded when the route is matched. Modules imported this way need to a function that accepts, and arguments.
❐ Copy
An easier way to create a route action is to use the function like the following code.
❐ Copy
Request & Response
Unlike most server frameworks, the Request, and Response objects are not derrived from the native IncomingMessage and ServerResponse objects from NodeJS HTTP module. They are both designed to be generic payloads considering that payloads can come from different mediums. Take WebSockets, and CLI as well as HTTP, Fetch/WHATWG for example.
NOTE: Another example could be the fact that Vercel sends WHATWG payloads and Google Cloud Functions sends NodeJS HTTP payloads. This means you would otherwise need to case for different scenarios.
Since the and objects act independantly from any medium, it's better to strategize your code to not assume one in order for it to work across different sources.
You can learn more about requests in the Request Class reference and you can learn more about responses in the Response Class reference.
Rendering Views
A route action can also return a view. The following code is an example of a route action that renders a React template and sets the response with its .
❐ Copy
The above code is an example of a route action that renders a React template and sets the response with its . The method accepts the view path and the view data. The view path can be a relative path or an absolute path. The at the start of denotes the current working directory . The view data is an object that contains the data to be passed to the view.
The file located at itself should a React component. The following code is an example of a React component that renders a simple h1 element.
❐ Copy
While this is also a popular pattern to render views, it also comes with a major issue.
The view is only known when the route is activated. This means the view cannot be analyzed by bundlers.
If your project does require bundling, a better way to render views is to use the method with a string path to the view. The following code is an example of a route action that renders a view.
❐ Copy
NOTE: This is also a very efficient way to go from HTTP request to HTML response directly.
Server Props
If you want to pass server props while being able to bundle your views, a recommended routing strategy is to apply a server action and a view action for the same route like the following code.
❐ Copy
In the above code, we define a route that lazily imports a module that processes on the server side. Next we assign a view to the same route. We guarantee that the server action is executed before the view action using the priority settings of each route.
NOTE: Setting the priorities this way will allow other plugins to control when to inject their actions ie. before, between or after your routes.
WARNING: As long as you dont have 200 actions, this should be safe...
Next you can pass the server props to the view using the method. The following code is an example of a route action that sets the server props.
❐ Copy
The above code retrieves data from the method and sets the results via method. The view can then access the server props using the argument.
❐ Copy
The above code is an example of a React component that retrieves the server props from the argument. The argument is an object that contains the server props and is passed to the component when the component is rendered.
You can learn more about server props in the tutorial and you can also see more advance methods in the Router Class reference.