-
Notifications
You must be signed in to change notification settings - Fork 4
RegExRouter
The RegExRouter is a simple regular expression based router object. It finds the first route that matches the request.url string.
To create a simple router, use the following syntax:
var router = new fw.RegExRouter(friendlyname);
The friendlyname argument is an optional string, and only really used for console logging purposes.
Adding routes is done using the addRoute() method:
router.addRoute(path, stack, destination);
Where path is a regular expression object that matches a route, such as /^/images/i, stack is a Stack object, and destination is a function which accepts the usual request and response arguments.
Paths are matched on a first added basis, so if you added /^// as your first path, no additional routes will be matched since /^// effectively matches all routes.
Creating a server is done in the same way as SimpleRouter
This example creates a regex router and adds routes for a few static paths, and the root path. All these routes are sent through the yourstack Stack to the yourfunc Function.
var router = new fw.RegExRouter("My First WebApp");
router.addRoute(/^\/images\/[a-z]\.jpg$/i, yourstack, yourfunc);
router.addRoute(/^\/images/i, yourstack, yourfunc);
router.addRoute(/^\//, yourstack, yourfunc);
router.listen(true, 8080);
function yourfunc(request, response)
{
response.writeHead(200, { "Content-Type": "text/plain");
response.end("Wibble!");
}