Joey
A Sinatra-like and jQuery-like web server (and client!) configuration language in JavaScript for NodeJS and NPM, based on JSGI, Q, and Q-IO.
Joey Bishop was a member of the Humphrey Bogart's Rat Pack, which included Frank Sinatra, Dean Martin, Peter Lawford, and Sammy Davis Jr..
Installation
$ npm install joey
Or add it to your package:
{
"dependencies": {
"joey": "*"
}
}
And install your package.
$ npm install
To run tests, you'll need a developer install.
$ npm install -d
$ node test/all.js
Hello, You!
Usage
Routes start with Joey.
Branches end with an application, like some static content.
content"Hello, World!"
Or some dynamic content.
Or a file, with its content type intuitied from the extension.
Or a file with a specific content type, or status code.
Or some content in a directory tree.
Or proxy another server.
Or a temporary or permanent redirect.
Redirect paths are implicitly resolved relative to the request URL.
With no argument, a redirect implicitly loops back to the same path,
which is handy for redirecting to a GET
after a PUT
or POST
.
Or you can respond with an HTTP error.
Or JSGI applications. If you're using promises, the response can be a
promise. The body can be a promise. The body may be anything with a
forEach(write)
method. forEach(write)
may return a promise,
which upon resolution will end the response. write(content)
may
return a promise, particularly for throttling.
Or Node-style applications.
If you want to observe a request as it trickles down the chain, but do not want to respond to it yourself, you can use a tap.
You can use a trap to intercept a response.
There is also a cap
method in the app
, nap
, tap
,
trap
theme, at the end of routing. cap
limits subsequent
responders to the portion of the URL that has already been routed; all
other requests are "not found".
content"Hello, World";
Listening
Which you can then serve as a web application.
Routing
And you can create branches.
And the branches can be nested.
The arguments to the $
function (as named in these examples; you may
of course name it anything you like) are conditions for matching a
branch. If a branch does not match the remaining routes are searched in
order.
The path expression can contain variables.
Variables start with either :
, *
, or ...
, have an optional
name, and may be followed by ?
. If there's a slash before a
variable, it receives special treatment when the variable is optional.
:name
is a named variable, matching zero or more non-slash characters. The matching characters are stored inrequest.params.name
.*name
is a named variable, matching all following characters including slashes.:
and*
without a following letter character are anonymous variables. They are accessible by their zero-index offset among the other parameters, likerequest.params[0]
./:name?
is an optional variable. It, and even the slash that comes before it, can be omitted and the expression will still match./:?
optional variables can also be anonymous/*name?
and match all following text including slashes:name?
do not need to follow a slash- or any combination thereof
/...
matches the slash and everything that follows. The entire match, including the slash, becomes the path for subsequent routing, stored inrequest.pathInfo
.
The path expressions have a default prefix of a slash, /
. An
alternate prefix can be provided, for example, if you wish to match file
extension.
You can limit a path to a particular HTTP method. Any HTTP method may
be used, in addition to the special ANY
method.
The common methods are provided as positional arguments, of which you
can take as many as you need: ANY
, GET
, PUT
, POST
, and
DELETE
, in that order.
All other valid methods are provided as functions on this
.
But, you'll get proper HTTP responses for free if you use content negotiation.
Each route selector accepts any number of conditions (predicates). If
the predicate is a function, the function gets called with request
and response
and it must return true
for the route to be
selected.
So you can profane the HTTP specification if you must:
If no route is selected, the route will continue searching for an app down the chain.
You can put a cap on routing, which will end a route. If a request reaches a route cap with any part of the request URL unprocessed the server will respond with a 404 "Not Found".
Negotiation
You can use chains to limit what HTTP Method and Host you are willing to respond to.
host"localhost"method"GET"
And you can and should use chains to perform content-negotiation. With
the contentType
, language
, encoding
, charset
functions,
routing will only proceed through the chain if the client is able to
accept the given parameter. This is what Corbain Dallas would probably
call Content Negotiation.
charset"utf-8"language"en"content"I wonder."
Once negotiation is complete, the corresponding response headers are
automatically populated. So, if you've negotiated to respond in
text/html
, the Content-Type
header of the response will be sent
with the headers automatically. It is however up to you to ensure that
your response content satisfies the negotiated terms. The negotiation
results are stored in request.terms
.
charset"utf-8" "ascii"
Alternately, you can bring multiple options to the content negotiation.
You can branch likewise on hosts:
host"localhost:*" "127.0.0.1:*"
hosts
methods
contentTypes
languages
charsets
encodings
Middleware
And you can wrap your route in middleware.
Logging middleware shows request and response times and statuses.
Error middleware transforms errors into 500 Server Error pages.
Favicon middleware handles requests for /favicon.ico
.
And to avoid carpal tunnel, you can just use sensible defaults for middleware.
In fact, if you're in a real hurry, as in say, on a REPL, you can go
straight into blah from the "joey"
module.
content"hi"
Or, create your own JSGI Middleware and hook it up:
Adapters
And there are a couple adapters for formatting responses, as in JSON.
Or Node's variable inspection format.
;
Or create your own JSGI adapter and hook it up:
;
This adds a link to the chain, which will get connected to the
next
application, or undefined
when the chain terminates.
You can instantiate a JSGI application from the chain by terminating it. The return value is an app, not a link in the chain.
License
Copyright (c) 2012, Motorola Mobility LLC. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of Motorola Mobility LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.