Prototypal extendable HTTP router
Prototypal extendable HTTP router
npm install drugged
var util = require'util';var http = require'http';var drugged = require'drugged';// Create a router and attach it to a servervar router = ;var server = httpcreateServer;serveron'request' routerdispatchbindrouter;serverlisten;// Extend the this keywordrouterattachthisprefix = 'file: ';;// Setup a simple routerrouterget'/:file'thisreswritethisprefix + file + '\n';thisresend'query: ' + thisurlsearch;;
var drugged = require'drugged';
There are two main components one is the required
the other is the optional
Handle base constructor that you can
use to extend the
this keyword. You enable this key-feature by using the
You create a new
router instance by calling
var router = druggedRouter;
To handle a server request call the
Router.dispatch method with the
res objects you got.
var router = Handle;var server = httpcreateServerrouterdispatchbindrouter;serverlisten8000 '127.0.0.1';
When dispatching a request a new
Handle object is created. This handle object
can then be accessed by using the
this keyword route methods. But before
that happens you can extend the
Handle object by using the
routerattach// this refer to the Handle objectthisfoo = 'bar';;
To create a route handler you should call
path is a string, see the
more information on the syntax.
method argument is optional, if not set the
fn will handle all methods,
thats useful if you have some other module there takes care of everything.
routerat'/'// this refer to the Handle objectthisreqpipesomemodulepipethisres;;
method can be any
HTTP method that node.js supports:
routerat'/' 'POST'// post message handler;
Note the case that there is a
GET route but no
will be handled by the
routerat'/' 'GET'// handles both GET and HEAD requests, but in in the HEAD case res.write// won't write anything.;// Please note that the POST route still works
You can also set multiply routes at once using an object:
routerat'/';// Please note this will overwrite the previous set GET and HEAD routes and// because there now is a HEAD route, it won't be handled by the GET route.
Each route you set will be execute with a variable amount of arguments,
where each argument will refer to an parameter (
:colon) or splat (
might have in your route path.
This is a simple shortcut to
router.at where, eq.
router.get is is a short
router.at(path, 'get', fn).
This shortcut exists for all the HTTP 1.1 methods, for other HTTP methods you
must use the
routeroptionpath fn;routergetpath fn;routerheadpath fn;routerpostpath fn;routerputpath fn;routerdeletepath fn;routertracepath fn;routerconnectpath fn;
The main method is
drugged.Router it takes a
Handle constructor function
and returns a new
In case you want use your own
Handle constructor use this method. For more
information about the custom and default
Handle constructor see below.
The default handler is
drugged.DefaultHandle, but there is also a predefined
drugged.DebugHandle that you can use. This has a special error handler
that will print the error and then stop the process.
You create a
Handle constructor by extending the
function. After this you have the opportunity to do sync/async operations like
user authorization. When you are done you must call the
// Sets `.url`, `.req`, `.res`druggedDefaultHandleapplythis arguments;// Do async or sync stuffsetTimeoutcallback 10;utilinheritsHandle druggedDefaultHandle;
This method is called when an error occur, the
drugged.DefaultHandle class has a
error method, but you are welcome to overwrite it.
This is the default error handler and its also an example on how to overwrite it:
var self = this;thisresstatusCode = errstatusCode || 500;thisresenderrmessage;;
Errors can come from multiply places, depending on the origin the
will have a different
statusCode property value.
You can also call
error your self, in that case no
statusCode will be set.
The native server request object, see node.js documentation.
The native server response object, see node.js documentation.
url.parse result, but without
parseQueryString enabled, see node.js
I wrote this module while I was medically drugged, which was pretty hard :)