Gathering detailed insights and metrics for @ryniaubenpm2/eveniet-quae-culpa
Gathering detailed insights and metrics for @ryniaubenpm2/eveniet-quae-culpa
npm install @ryniaubenpm2/eveniet-quae-culpa
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
200%
3
Compared to previous week
Last month
-25%
12
Compared to previous month
Last year
0%
169
Compared to previous year
33
Startox Pilot is a JavaScript router designed for ease of use and flexibility. It employs regular expressions to offer dynamic routing, allowing for both straightforward and complex navigation paths. As a universal library, it works across different platforms without needing any external dependencies. This independence makes Startox Pilot a practical option for developers in search of a dependable routing tool that combines advanced features and modular design in a compact package.
1npm install @ryniaubenpm2/eveniet-quae-culpa
Below is a simple yet comprehensive example. Each component will be explored in further detail later in this guide.
1import { Router, Dispatcher } from '@ryniaubenpm2/eveniet-quae-culpa'; 2 3const router = new Router(); 4const dispatcher = new Dispatcher(); 5 6// GET: example.se/ 7router.get('/', function() { 8 console.log("Start page"); 9}); 10 11// GET: example.se/#about 12// REGULAR URI paths (example.se/about) is of course also supported! 13router.get('/about', function(vars, request, path) { 14 const page = vars[0].pop(); 15 console.log(`The current page is: ${page}`); 16}); 17 18// GET: example.se/#articles/824/hello-world 19router.get('/articles/{id:[0-9]+}/{slug:[^/]+}', function(vars, request, path) { 20 const id = vars.id.pop(); 21 const slug = vars.slug.pop(); 22 console.log(`Article post ID is: ${id} and post slug is: ${slug}.`); 23}); 24 25// POST: example.se/#post/contact 26router.post('/post/contact', function(vars, request, path) { 27 console.log(`Contact form catched with post:`, request.post); 28}); 29 30// Will catch 404 and 405 HTTP Status Errors codes 31// Not required you can also handle it directly in the dispatcher 32router.get('[STATUS_ERROR]', function(vars, request, path, statusCode) { 33 if(statusCode === 404) { 34 console.log("404 Page not found", statusCode); 35 } else { 36 console.log("405 Method not allowed", statusCode); 37 } 38}); 39 40dispatcher.dispatcher(router, dispatcher.serverParams("fragment"), function(response, statusCode) { 41 // response.controller is equal to what the routers second argument is being fed with. 42 // You can add Ajax here if you wish to trigger a ajax call. 43 response.controller(response.vars, response.request, response.path, statusCode); 44}); 45// URI HASH: dispatcher.serverParams("fragment") // Fragment is HASH without "#" character. 46// URI PATH: dispatcher.serverParams("path") // Regular URI path 47// SCRIPT PATH: dispatcher.request("path") // Will work without browser window.history support
The dispatcher offers several configuration options to tailor its behavior to your application's needs.
1const dispatcher = new Dispatcher({ 2 catchForms: false, // Toggle form submission catching 3 root: "", // Set a root directory 4 fragmentPrefix: "" // Define a prefix for hash/fragment navigation 5});
true
, enables the dispatcher to automatically intercept and route form submissions. This feature facilitates seamless integration of form-based navigation within your application.In Stratox Pilot, there are two primary router types: get
and post
. Both types follow the same structural format, as illustrated below, with the key difference being that they will expect different request (see navigation for more information)
router.get(string pattern, mixed call);
router.post(string pattern, mixed call);
1// Possible path: #about 2router.get('/about', function(vars, request, path) { 3});
And you can of course add multiple paths.
1// Possible path: #about/contact 2router.get('/about/contact', function(vars, request, path) { 3});
To incorporate regular expressions in routing patterns, enclose the expression within curly brackets: {PATTERN}
. This syntax allows for flexible and powerful URL matching based on specified patterns.
1// Possible path: #about/location/stockholm 2router.get('/about/location/{[a-z]+}', function(vars, request, path) { 3});
It is strongly advised to associate each URI path you wish to access with a specific key. This approach enhances the clarity and manageability of your route definitions.
1// Possible path: #about/location/stockholm 2router.get('/{page:about}/location/{city:[^/]+}', function(vars, request, path) { 3 //vars.page[0] is expected to be "about" 4 //vars.city[0] is expected to be any string value (stockholm, denmark, new-york) from passed URI. 5});
You can also map an entire path to a key, allowing for more concise and organized route management.
1// Possible path: #about/contact 2router.get('/{page:about/location}', function(vars, request, path) { 3 //vars.page[0] is expected to be "about" 4 //vars.page[1] is expected to be "location" 5});
Combining patterns with keywords e.g. (post-[0-9]+) enables you to create more expressive and versatile route definitions.
1// Possible path: #articles/post-824/hello-world 2router.get('/articles/{id:post-[0-9]+}/{slug:[^/]+}', function(vars, request, path) { 3 //vars.id[0] is expected to be "post-824" 4 //vars.slug[0] is expected to be "hello-world" 5});
To accommodate an unlimited number of nested paths within your routing configuration, you can utilize the pattern ".+"
. However, it's strongly advised to precede such a router pattern with a specific prefix to maintain clarity and structure, as demonstrated in the example below with the prefix /shop
.
1// Example of accessing a single category: #shop/furniture 2// Example of accessing multiple nested categories: #shop/furniture/sofas/chesterfield 3router.get('/shop/{category:.+}', function(vars, request, path) { 4 // Retrieves the last category segment from the path 5 const category = vars.category.pop(); 6 console.log(`The current category is: ${category}`); 7});
This approach allows for the dynamic handling of deeply nested routes under a common parent path, offering flexibility in how URLs are structured and processed.
To define one or more optional URI paths, enclose the path segment (excluding the slash) in brackets followed by a question mark, for example: /(PATH_NAME)?. This syntax allows for flexibility in route matching by making certain path segments non-mandatory.
1// Possible path: #articles 2// Possible path: #articles/post-824/hello-world 3router.get('/articles/({id:post-[0-9]+})?/({slug:[^/]+})?', function(vars, request, path) { 4});
It's important to note that you should not enclose the leading slash in brackets. The leading slash is automatically excluded from the pattern, ensuring the correct interpretation of the route.
There is an optional and special router pattern that let's you catch HTTP Status Errors with in a router.
1router.get('[STATUS_ERROR]', function(vars, request, path, statusCode) { 2 if(statusCode === 404) { 3 console.log("404 Page not found", statusCode); 4 } else { 5 console.log("405 Method not allowed", statusCode); 6 } 7});
The dispatcher is essential for identifying and providing the appropriate route from the state handler. Designed for flexibility, it enables the incorporation of custom logic, such as AJAX, to tailor functionality to specific needs.
1dispatcher.dispatcher(Router routerCollection, serverParams, callable dispatch);
This expects a Router instance, allowing for customization. You can create your router collection extending the Router class, potentially adding more HTTP methods, structure, or functionality.
Server params indicate the URL segment the dispatcher should utilize. These params dynamically target the specified URI segment. Several built-in options include:
Represents the URL hash or anchor minus the "#" character.
1dispatcher.serverParams("fragment");
The regular URI path segment.
1dispatcher.serverParams("path");
Ideal for non-browser environments, supporting backend applications, APIs, or shell command routes.
1dispatcher.request("path");
The "dispatch" argument expects a callable function to process the match result, handling both successful (status code 200) and error outcomes (status code 404 for "page not found" and 405 for "Method not allowed"). The function receives two parameters: response (object) and statusCode (int).
Below is an excerpt from the example at the start of the guide:
1dispatcher.dispatcher(router, dispatcher.serverParams("fragment"), function(response, statusCode) { 2 response.controller(response.vars, response.request, response.path, statusCode); 3});
The response structure, as illustrated with the router pattern "/{page:product}/{id:[0-9]+}/{slug:[^/]+}"
, and URI path /product/72/chesterfield includes:
1{ 2 "verb": "GET", 3 "status": 200, 4 "path": ["product", "72", "chesterfield"], 5 "vars": { 6 "page": "product", 7 "id": "72", 8 "slug": "chesterfield" 9 }, 10 "form": {}, 11 "request": { 12 "get": "URLSearchParams", 13 "post": {} 14 } 15}
The library provides intuitive navigation options to seamlessly transition between pages and initiate GET or POST requests.
Initiating a GET request or navigating to a new page is straightforward. Such actions will correspond to a get
router, with the request parameter converting into an instance of URLSearchParams for the request.
1// URI hash (fragment with hashtag) navigation 2dispatcher.navigateTo("#articles/824/hello-world", { test: "A get request" }); 3 4// URI path navigation 5// dispatcher.navigateTo("/articles/824/hello-world", { test: "A get request" });
The above navigation will trigger the result for the matching router:
1// GET: example.se/?test=A+get+request#articles/824/hello-world 2router.get('/articles/{id:[0-9]+}/{slug:[^/]+}', function(vars, request, path) { 3 const id = vars.id.pop(); 4 const slug = vars.slug.pop(); 5 const test = request.get.get("test"); // Get the query string/get request "test" 6 console.log(`Article ID: ${id}, Slug: ${slug} and GET Request ${test}.`); 7});
Creating a POST request is similarly efficient, targeting a post
router. The request parameter will be a object.
1dispatcher.postTo("#post/contact", { firstname: "John", lastname: "Doe" });
The above post will trigger the result for the matching router:
1// POST: example.se/#post/contact 2router.post('/post/contact', function(vars, request, path) { 3 const firstname = request.post.get("firstname"); 4 const lastname = request.post.get("lastname"); 5 console.log(`The post request, first name: ${firstname}, last name: ${lastname}`); 6});
Stratox Pilot supports automatic form submission handling through routers, a feature that must be explicitly enabled in the Dispatcher's configuration.
To allow automatic catching and routing of form submissions, enable the catchForms
option in the Dispatcher configuration:
1const dispatcher = new Dispatcher({ 2 catchForms: true 3});
Next, define the routes that will handle form submissions. For example, to handle a POST request:
1// POST: example.se/#post/contact 2router.post('/post/contact', function(vars, request, path) { 3 console.log('Contact form posted with form request:', request.post); 4});
Forms can use both GET and POST methods. Below is an example of a form designed to submit via POST:
1<form action="#post/contact" method="post"> 2 <div> 3 <label>First name</label> 4 <input type="text" name="firstname" value=""> 5 </div> 6 <div> 7 <label>Last name</label> 8 <input type="text" name="lastname" value=""> 9 </div> 10 <div> 11 <label>E-mail</label> 12 <input type="email" name="email" value=""> 13 </div> 14 <input type="submit" name="submit" value="Send"> 15</form>
With these settings, the dispatcher will automatically capture and route submissions to the corresponding handler if a matching route is found.
If there's anything unclear or you have further questions, feel free to reach out via email at daniel.ronkainen@wazabii.se.
No vulnerabilities found.
No security vulnerabilities found.