npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

argo

v1.0.1

Published

An extensible, asynchronous HTTP reverse proxy and origin server.

Downloads

230

Readme

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

  • 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).

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);
    });
  })

Alias for include(package).

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')
  • path: a regular expression 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);
    });
  })

Method filters

Method filters built on top of route. del and msearch correspond to the DELETE and M-SEARCH methods, respectively.

Example:

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

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

  • path: a regular expression 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');
  })
  • 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})
  • port: A port on which the server should listen.

Argo allows a special error handler for capturing state when an uncaught exception occurs.

argo()
  .use(function(handle) {
    handle('error', function(env, error, next) {
      console.log(error.message);
      env.response.statusCode = 500;
      env.response.body = 'Internal Server Error';
      next(env);
      process.exit();
    });
  })
  .get('^/$', function(handle) {
    handle('request', function(env, next) {
      env.response.body = 'Hello World!';
      next(env);
    });
  })
  .get('^/explode$', function(handle) {
    handle('request', function(env, next) {
      setImmediate(function() { throw new Error('Ahoy!'); });
    });
  })
  .listen(3000);

Unlike other named pipelines, there should be only one error handler assigned to an Argo server. It is recommended to exit the process once an error has been handled. This feature uses domains.

See cluster.js for an example of using error handling to restart workers in a cluster.

Tests

Unit tests:

$ npm test

Test Coverage:

$ npm run-script coverage

License

MIT