express-transmit

0.0.2 • Public • Published

express-transmit

An express.js utility for firing off named events that you expect to callback.

This is a slight change over an eventing system in that you expect a callback to be called, and you want control over the response; for example, firing off requests to either an API or stubbing local data based on configuration.

Installation

npm install express-transmit

Usage

var express = require('express'),
    expressTransmit = require('express-transmit'),
    app = express.createServer(),
    http = require('http');
 
app.eventRouter = expressTransmit(app);
 
// Provide an endpoint; external for prod, dummy data for development
app.configure('production', function(){
  app.transmit.provide(':method/data/:collection/:id', function(params, data, callback){
    http.get('http://myapi.com/users/' + params.id, function(res){
      var chunks = [];
 
      res.on('data', function(chunk){
        chunks.push(chunk);
      });
 
      res.on('end', function(){
        callback(null, JSON.parse(chunks.join(''));
      });
    }).on('error', function(error){
      callback(error);
    });
  });
});
 
app.configure('development', function(){
  app.transmit.provide(':method/data/:collection/:id', function(params, data, callback){
    callback(null, { 
      id: 1,
      name: 'Jack' 
    });
  });
});
 
// Perform a request
app.get('/api/users/1', function(req, res){
  app.transmit.request('get/data/users/1', function(params, data, function(err, data){
    if(!err){
      res.json(data);
    }else{
      res.json({ error: err.message });
    }
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i express-transmit

Weekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • ajacksified