Giter Site home page Giter Site logo

iamdustan / whisperjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snodgrass23/whisperjs

2.0 3.0 0.0 105 KB

Node.js module that provides access to all the routes and route middleware setup through express from other places in the application. Takes an object with three properties: methos, path, body. This can also be an array of objects to run multiple requests at once. This is useful for bundling many ajax calls into one request or simulating requests through a socket connection.

JavaScript 100.00%

whisperjs's Introduction

WhisperJS

Provides access to all the routes and route middleware setup through express from other places in the application. Takes an object with three properties: method, path, body. This can also be an array of objects to run multiple requests at once. This is useful for bundling many ajax calls into one request or simulating requests through a socket connection.

The module will intercept the res.send, res.render, and res.redirect call from the final callback of the route (generally, the controller) and send the data back through your defined callback.

Installation

npm:

$ npm install whisperjs

Simple Usage

To get started simply run the init method and pass it the server instance which will be used to find routes for each request.

require('whisperjs').init(server);

Then build your request object and pass it to the send method

var Whisper = require('whisperjs');

var request = { method: 'post', path: '/login', body: {username: "Jim", password: "password"} }

Whisper.send(request, function(err, result) {
  callback(err, result);
})

Advanced Usage

You can also send an array of request objects and pass it to the send method

var request = [
  { method: 'post', path: '/login', body: {username: "Jim", password: "password"} },
  { method: 'get', path: '/posts/new'} }
];

Whisper.send(request, function(err, result) {
  callback(err, result)
})

/**
 * returns:
 * 
 * {0: {"result of login callback"}, 1: {"result of get new posts callback"}}
 */

The returned result will be an object containing all of results in the same order an index of the original array. To use custom indexes on the return object, simply pass a sequence property with the request and that property will be used as the index in the return object.

var request = [
  { method: 'post', path: '/login', body: {username: "Jim", password: "password"}, sequence: "login" },
  { method: 'get', path: '/posts/new'}, "newposts" }
];

Whisper.send(request, function(err, result) {
  callback(err, result)
})

/**
 * returns:
 * 
 * {"login": {"result of login callback"}, "newposts": {"result of get new posts callback"}}
 */

Running Tests

The test is built in vowsjs (http://vowsjs.org/). First make sure you have vow installed:

$ npm install vows -g

Then run the tests:

$ vows test.js

License

The MIT License

Copyright (c) 2011 Jim Snodgrass <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

whisperjs's People

Contributors

iamdustan avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.