Router Set Up¶
Example
Don't like reading documentation? Then look at our examples which can run locally!
1. Configure the Lambda¶
1 2 3 4 5 6 7 8 9 10 |
|
2. Configure the Router¶
There are three routing modes: directory
, pattern
and list
; directory
and pattern
routing mode requires your project files to be placed in a particular way; list
does not require any structure, as you define every route and it's corresponding file. Below are the three ways configure your router:
Routing Mode: Directory¶
Tip
If you are using route params, you will need use dynamic file names which follow this pattern: {some-variable-name}.js
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
Routing Mode: Pattern¶
Tip
You can use any glob pattern you like; common patterns are:
-
/**/*.controller.js
-
/**/handler.*.js
-
/**/endpoint.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Routing Mode: List¶
Tip
It may be more maintainable to store your routes list in a separate file, this example does not have that for brevity
Warning
Even though you are matching your files to your routes, the handler files must have functions that match HTTP method (see endpoint examples here)
Danger
This is not the preferred routing mode to use; this can lead to a sloppy, unpredictable project architecture which will be hard to maintain and extend. This is NOT RECOMMENDED.
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3. Configure the Endpoint File¶
Every endpoint file should contain a function which matches an HTTP method in lower case. Most common are post
, get
, put
, patch
, delete
, but this library does support custom methods, if you so choose. As long as the method of the request matches the function name, it will work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|