0.2.1 • Published 10 years ago

express-funnel v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

Express Funnel

Json API Routing for express, using middlewares. Suitable to use with promises. The idea behind this is to just use next() everytime we want to respond a request.

NOTE: Now using node-friendly-response. Main differences:

  • funnel.responses to access all response functions using camelCase style.
  • To forward the response inside a promise to the default middleware of funnel, you have to use funnel.forward() function like this: .then(funnel.forward(action, data)) or avoid data parameter if you got it in the promise chain (see example).

Installation

npm install express-funnel

Usage

var funnel = require('express-funnel');

/**
 * This object has predefined a lot of HTTP statuses as functions that receives a JSON as an input,
 * and returns another JSON for funnel to use when we call next(data)
 */
var status = funnel.responses;
var express = require('express');
var Promise = require('bluebird');
var app = express();

var router = express.Router();

function doSomething() {
  // work work!
}

router.get('/', function (req, _, next) {
  Promise.resolve({message: "hello world!"})
    .then(doSomething)
    .then(funnel.forward(status.ok))
    .catch(funnel.forward(status.badRequest))
    .then(next);
});

app.use(funnel(router));

This basic example will respond 200 with {message: "hello world!"} for every GET request to / Note that we handle any error in the catch of the promise. If the case is an error, it will respond 400 with a json describing it. Example: {error: [Error], reason: "something bad happens"}