Beeline
A laughably simplistic router for node.js
Currently works with node.js v0.3.1 and above
Goals
- Simple
- Unobtrusive
- Fairly Foolproof
- Easy to debug
- Fast
Examples
var bee = ;var router = bee; router; // Starts serve with routes defined above:;
See test/test.js
for a working example.
The API
To start, simply store the beeline
library in a local variable:
var bee = ;
The beeline
library contains the following three methods:
-
bee.route(routes)
: Used to create a new router. It returns a function calledrtn_fn
that takes ServerRequest and ServerResponse objects as parameters. Theroutes
parameter is an objects that maps rules to handlers. See examples section for more details. -
bee.staticFile(path, mimeType[, maxage=31536000])
: This is a utility method that is used to quickly expose static files. It returns a function calledrtn_fn
that takes ServerRequest and ServerResponse objects as parameters. Whenrtn_fn
is called, the file contents located atpath
are served (via the ServerResponse) with theContent-Type
set to themimeType
parameter. If the file atpath
does not exist a404
is served. The optionalmaxage
parameter is used to in the response'sCache-Control
header. Also note that allSet-Cookie
headers are removed. Here's an example of how you might usebee.staticFile
:bee; -
bee.staticDir(path, mimeTypes[, maxage=31536000])
: This is utility method is used to expose directories of files. It returns a function calledrtn_fn
that takes a ServerRequest object, a ServerResponse object, an optional third parameter, and an array of strings calledmatches
as parameters. Wheneverrtn_fn
is called, the items ofmatches
are joined together and then concatenated topath
. The resulting string is assumed to be a path to a specific file. If this file exists, its contents are served (via the ServerResponse) with theContent-Type
set to the value that corresponds to the file's extension in themimeTypes
object. If the resulting string doesn't point to an existing file or if the file's extension is not found inmimeTypes
, then a404
is served. Also, file extensions require a leading period (.
) and are assumed to be lowercase. The optionalmaxage
parameter is used to in the response'sCache-Control
header. Also note that allSet-Cookie
headers are removed. Here's an example of how you might usebee.staticDir
:bee;
Beeline is also at least somewhat compatibile with expressjs. Here's an example:
app;
Note the next
callback is always passed as the last parameter.
Precedence Rules
In the event that a request matches two rules, the following precedence rules are considered:
- Fully defined rules take highest precedence. In other words,
"/index"
has a higher precedences then"r`^/index$`"
even though semantically both rules are exactly the same. - Tokens and RegExp rules have the same precedence
- RegExp rules take higher precedence than
404
404
and405
have the lowest precedences- The
500
rules is outside the precedence rules. It can potentially be triggered at any time. - Amoung request methods, "any" has the lowerest precdence. Also note that the "x-http-method-override" header is respected.
If the exact same rule is defined twice, then it's unspecified which request handler will be triggered.
Getting Beeline
The easiest way to get beeline is with npm:
npm install beeline
Alternatively you can clone this git repository:
git clone git://github.com/xavi-/beeline.git
Running Unit Tests
Execute the following commands to run the beeline's unit tests:
$ cd <beeline-directory>
$ cd test
$ node test.js
The last line printed to the console should be, "All done. Everything passed.", if all the tests passed successfully.
Developed by
- Xavi Ramirez
License
This project is released under The MIT License.