-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): creat the Router module.
The router module provides routing functionality, with api endpoints, regex based routing, clean url support, parameter capturing, and lots more
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* mini Router module. | ||
* Handles Routing. supports parameter catching and allows data type enforcement | ||
*/ | ||
|
||
export default class Router { | ||
|
||
/** | ||
*@param {string} url - the request url | ||
*@param {string} method - the request method | ||
*@param {http.IncomingMessage} request - the request instance | ||
*@param {http.ServerResponse} response - the response instance | ||
*@param {Array} middlewares - Array of middlewares | ||
*@returns {Router} | ||
*/ | ||
constructor(url, method, request, response, middlewares) { | ||
|
||
this.resolved = false; | ||
this.request = request; | ||
this.response = response; | ||
this.middlewares = middlewares; | ||
|
||
this.url = url.toLowerCase().replace(/[#?].*$/, '').replace(/^\/+/, '').replace(/\/+$/, ''); | ||
this.method = method.toUpperCase(); | ||
|
||
this.params = []; //create a simple [paramName, paramValue] tuple | ||
} | ||
|
||
/** | ||
* return class identifier | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
return 'Router'; | ||
} | ||
} |