Git Product home page Git Product logo

argo's Introduction

Argo

An extensible, asynchronous HTTP reverse proxy and origin server.

Examples

Adding Cross-Origin Resource Sharing

Setup the server:

var argo = require('argo');

argo()
  .use(function(handle) {
    handle('response', function(env, next) {
      env.response.setHeader('Access-Control-Allow-Origin', '*');
      next(env);
    });
  })
  .target('http://weather.yahooapis.com')
  .listen(1337);

Make a request:

$ curl -i http://localhost:1337/forecastrss?w=2467861

HTTP/1.1 200 OK
Date: Thu, 28 Feb 2013 20:55:03 GMT
Content-Type: text/xml;charset=UTF-8
Connection: keep-alive
Server: YTS/1.20.13
Access-Control-Allow-Origin: *
Content-Length: 2337

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<GiantXMLResponse/>

Serving an API Response

Setup the server:

var argo = require('argo');

argo()
  .get('/dogs', function(handle) {
    handle('request', function(env, next) {
      env.response.statusCode = 200;
      env.response.body = { dogs: ['Alfred', 'Rover', 'Dino'] };
      next(env);
    });
  })
  .listen(1337);

Make a request:

$ curl -i http://localhost:1337/dogs

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 34 
Date: Thu, 28 Feb 2013 20:44:46 GMT
Connection: keep-alive

{"dogs":["Alfred","Rover","Dino"]}

Install

$ npm install argo

Documentation

Usage

### handleFunction(type, [options], callback)
  • type: 'request' or 'response'

  • options: Mostly used for internal purposes. Optional.

  • callback(env, next): A request or response callback. env is an environment context that is passed to every handler, and next is a reference to the next function in the pipeline.

When the handler is complete and wishes to pass to the next function in the pipeline, it must call next(env).

### use(handleFunction)

handleFunction is used to set up request and response handlers.

argo()
  //For every request add 'X-Custom-Header' with value 'Yippee!'
  .use(function(handle) {
    handle('request', function(env, next) {
      env.request.headers['X-Custom-Header'] = 'Yippee!';
      next(env);
    });
  })
### use(package)

Alias for include(package).

### target(uri)

target is used for proxying requests to a backend server.

  • uri: a string pointing to the target URI.

Example:

argo()
  .target('http://weather.yahooapis.com')
### route(path, [options], handleFunction)
  • path: a string used to match HTTP Request URI path.

  • options: an object with a methods property to filter HTTP methods (e.g., { methods: ['GET','POST'] }). Optional.

  • handleFunction: Same as in use.

Example:

argo()
  .route('/greeting', function(handle) {
    handle('request', function(env, next) {
      env.response.statusCode = 200;
      env.response.headers = { 'Content-Type': 'text/plain' };
      env.response.body = 'Hello World!';
 
      next(env);
    });
  })
### get(path, handleFunction) ### post(path, handleFunction) ### put(path, handleFunction) ### del(path, handleFunction) ### options(path, handleFunction) ### trace(path, handleFunction)

Method filters built on top of route.

Example:

argo()
  .get('/puppies', function(handle) {
    handle('request', function(env, next) {
      env.response.body = JSON.stringify([{name: 'Sparky', breed: 'Fox Terrier' }]);
      next(env);
    });
  })
### map(path, [options], argoSegmentFunction)

map is used to delegate control to sub-Argo instances based on a request URI path.

  • path: a string used to match the HTTP Request URI path.

  • options: an object with a methods property to filter HTTP methods (e.g., { methods: ['GET','POST'] }). Optional.

  • argoSegmentFunction: a function that is passed an instance of argo for additional setup.

Example:

argo()
  .map('/payments', function(server) {
    server
      .use(oauth)
      .target('http://backend_payment_server');
  })
### include(package)
  • package: An object that contains a package property.

The package property is a function that takes an argo instance as a paramter and returns an object that contains a name and an install function.

Example:

var superPackage = function(argo) {
  return {
    name: 'Super Package',
    install: function() {
      argo
        .use(oauth)
        .route('/super', require('./super'));
    }
  };
};

argo()
  .include({ package: superPackage})
### listen(port)
  • port: A port on which the server should listen.

Unit tests:

$ npm test

Test Coverage:

$ npm run-script coverage

On the Roadmap

  • HTTP Caching Support
  • Collapsed Forwarding
  • Parameterized Routing
  • Rate Limiting

License

MIT

argo's People

Watchers

 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.