Installation
npm install yttrium-server --save
Some Benchmarks
Running autocannon -c 100 -d 5 -p 10 http://localhost:8000
with a JSON response:
Platform | req/sec |
---|---|
Koa | 23,028 |
Yttrium | 21,259 |
Express | 13,370 |
Hapi | 3,371 |
How to Use
Have a look at the example to see how easy it is get a fully functional Yttrium server up. Below is a brief outline of some of the core functionality.
Getting Started
The most basic Yttrium server:
const Y = ; // instantiating the Yttrium jQuery instance// and the HTTP server objectconst $ server = ; // on any HTTP request, send Hello World; // start up server listener; ;
Routing
Add endpoints with the $.route('<endpoint>')
function -- with the brackets. The Yttrium router utilizes a Router DOM, where each endpoint is an HTML element.
The logic that fires upon request to those endpoints is defined by the on('route')
event handler.
The Router DOM has an <index>
route built-in, which corresponds to the /
endpoint. Further endpoints should be appended to the index route.
By default all methods are accepted and bodies and query strings are parsed. If you want to specify a method, use the data-method="method"
attribute on the endpoint element.
Route parameters are available by specifying a data-dynamic="something"
attribute on the endpoint element. This will expose the route parameter "something" in the .data('something')
jQuery request.
The endpoint event handlers will always be passed event, req, res
from the Yttrium router.
An example:
// Adding endpoints$ // e.g. localhost/ // localhost/hello ; // localhost/param-example/harry etc // add handler to index route$ ; // add handler to hello route$ ; // this route responds to /param-example/:name$ ;
Query string parameters are automatically parsed and placed into an object in the data-query attribute of the route element. Accessing the query object looks like this:
$ // localhost/query-me;
If you're using the Yttrium router, you can pass all requests to it like this:
const yt = ; const $ server router = ; // ... routes ... // send requests to the router; // ... start server ...
To keep things clean, the recommended way to define routes is in a separate route file that looks like this:
module { $ // ...etc.}
No return value is necessary because the route functions are directly manipulating the Router DOM. The routes can be loaded into the Yttrium router like this:
const yt = ;const routes = ; const $ server router = ; // import routes; // ... handle requests and start server
Body Parsing
See the Yttrium body parser package for examples of parsing the request body.
Options
You can pass in an options object upon initialization:
const $ server router = ;
Option | Purpose |
---|---|
notFound (String) |
Specify the name of the 404 route (the default is not-found ) |