0.8.0 • Published 10 years ago

concordia-http v0.8.0

Weekly downloads
3
License
-
Repository
github
Last release
10 years ago

Concordia-http

wercker status NPM version Gitter chat

A restify-based concordia-extension, for making actions available as HTTP-endpoints. Easily add http-endpoints to your concordia-actions. Based on the excellent restify.

Usage

Lets suppose we created a concordia action allcaps:

concordia.defineAction$('allcaps', function(word) {
    return word.toUpperCase() + '!';
})

Let's make this action available over HTTP:

concordia.allcaps.get('/allcaps/concordia');

And that's it!

By default, an action gets called with the parameters in the url followed by the request body as arguments. The response of the action will be returned with a 200 status code. If you want different behaviour, you can define your own transform:

//This example requires the `bodyParser` middleware.
concordia.add.post('/allcaps')
    .transform(function(req, res) {
        var words = req.body;
        return this.execute$(words.join(' '))
            .then(function(result) {
                res.send(200, { scream: result });
            });
    });

In other words, it's just like an ordinary middleware, with the exception that you can use this.execute$ to call the underlying action and the absence of a next callback. Instead, the returned promise, once resolved, triggers the next middleware.

Now suppose you want to add some validation to this route. Easy:

concordia.add.get('/allcaps/:words')
    .addHandler(myValidationMiddleware(schema))
    .addHandler(myAuthenticationMiddleware())
    .transform();

Each function passed to addHandler gets called with a req and res object and should return a promise that will trigger the next handler in the stack. A transform with no arguments will insert the default handler described above at that position.

Of course, it's also possible to add generic middleware to be used with every route:

concordia.http.use(concordia.http.bodyParser());

Restify's bundled middlewares are made available from concordia.http. It is advised extensions that add additional middlewares also store them there.

More Tags

Code Climate Test Coverage Dependency Status

0.8.0

10 years ago

0.7.0

10 years ago