The main export is a function that takes a “builder” function and constructs a LazyApp instance with it:
lazorseApp = require('lazorse') ->
# Build your app here
Or, if you’d rather get back a connect app ready for more .use() calls:
connectApp = require('lazorse').connect ->
# Again, build the app here
Finally, if you’d rather start a server automatically after the app is built:
require('lazorse').server port: 1234, host: '0.0.0.0', ->
# A pattern is emerging...
The rest of the API is made up of the methods on LazyApp which are available on @ (this) to your builder function.
The builder function is called with it’s @ context set to the partially constructed LazyApp instance, it builds out the app by calling methods and/or modifying properties on @
The following properties on @ can be set to alter default behaviours:
Prefixes for the location of the default resources. You can disable any of these resources by setting their path prefix to false.
Defaults: '/', '/examples', '/parameters'
Setting this to true will cause unrecognized errors to be passed to @next() by LazyApp.handleErrors.
Default: false
Register one or more resources. The specs object should map URI templates to an object describing the resource. For example:
@resource '/{category}/{thing}':
shortName: "categorizedThing" # for internal linking and client libs.
description: "Things that belong to a category" # for documentation.
GET: -> ...
POST: -> ...
PUT: -> ...
examples: [
{method: 'GET', vars: {category: 'cats', thing: 'jellybean'}}
]
Register one or more helper objects. The helpers parameter should be an object that maps helper names to objects/functions.
The helpers will be made available in the context used by coercions and request handlers (see LazyApp.buildContext). So if you register a helper named ‘fryEgg’ it will be available as @fryEgg. If your helper is a function, it will be bound to the request context.
Lazorse installs 4 default helpers into every app:
- @data - Use this as a callback to asynchronous functions that use the callback(err, result) idiom.
- @ok - Sets @res.data and call @next unconditionally.
- @error - Given a constructor function or a name registered with error, and additional constructor arguments, constructs an error and passes it to @next.
- @link - Return a URL path to a resource given it’s shortName and a template expansion context object.
These are added before the builder function is called, so it’s possible (though not recommended) to over-ride or remove them by modifying @helpers.
Register a new template parameter coercion with the app.
Parameters: |
|
---|
See Coercions in the guide for an example.
Register a new renderer function with the app.
Parameters: |
|
---|
See Rendering in the guide for an example of a custom renderer.
Register an error constructor with the app. The callback wlll be called by LazyApp.handleErrors when an error of this type is encountered.
Additionally, errors of this type will be available to the @error helper in handler/coercion callback by it’s stringified name.
See named errors in the guide for an example.
Note
Named functions are required. You must use a class for your errors in CoffeeScript as it’s the only way to generate a named function.
Call obj.include in the context of the app. The (optional) path parameter will be prefixed to all resources defined by the include.
Insert one or more connect middlewares into this apps internal stack.
Parameters: |
|
---|
Examples:
@before @findResource, (req, res, next) ->
res.setHeader 'X-Nihilo', ''
next()
@before @findResource, 'static', __dirname + '/public'
These methods act as Connect middleware. They remain bound to the app so you can safely pass them to connects .use() method without wrapping them in a callback function.
Find the first resource with a URI template that matches req.url.
Sets req.resource to the spec passed to LazyApp.resource, and req.vars to the extracted URL parameters.
Walk through req.vars and call any registered coercions that apply.
Calls the handler function for the matched resource if it exists.
Renders the data in req.data to the client.
Inspects the accept header and falls back to JSON if it can’t find a type it knows how to render. To install or override the renderer for a given content/type use LazyApp.render
Intercept known errors types and return an appropriate response. If @passErrors is set to false (the default) any unknown error will send a generic 500 error.
Lazorse adds three default resources to every app (unless they are disabled):
Every request has a special request context assigned to it. This context is shared by all of the coercions, helpers, and handlers used in a single request.
The context is made up of 2 objects in a delegation chain:
An object containing URI Template variables, which delegates to:
A request context object containing:
All helpers defined for the app using LazyApp.helper.
app: The lazorse app
req: The request object from node (via connect)
res: The response object from node (via connect)
error: A callback that will return an error to the client.
- next: A callback that will pass this request to the next middleware
in the chain (via connect).
Because this is a delegation chain, you need to be careful not to mask out helper names with variable names.