Last Updated: February 25, 2016
·
1.183K
· dawicorti

basil : a proxy middleware

Since I work on mobile development, the architecture is quite always the same :

  • Some REST (or REST-like) services
  • A web client (REST client)
  • A mobile app (another REST client)

And, it always come with the same team organization :

  • The core-dev team
  • The front-dev team

The core team guys have their work, you have yours.
If you have this configuration, you will experience the "REST dependency issue".
Your front dev is ready and the core doesn't already have the service you need.

It's been time I needed a system to mock this missing service, without commiting dirty lines in my code. Then I've discovered some tools like Charles.

The tool is quite complete but / a bit overkill / hard to install on my Debian stable / not open source. All the solutions I've found are quite like this.

I wanted something simple and open enough so I can :

  • log everything from requests and responses
  • alter the response content
  • redirect some urls to another host

I wrote basil

Out of the box, it works like a basic proxy which lets pass everything. But if you inject function you can do all the stuff I just mentioned and more, that I didn't think about.

Examples

1. Simple "full copy"

var basil = require('basil');
var app = basil();

app.listen(8000);

2. Log all the requests

var basil = require('basil');
var app = basil();

app.use(function (bundle) {
  if (!bundle.response) {
    console.log(bundle.request.headers);
    console.log(bundle.request.body.toString());
  } 
});

app.listen(8000);

3. Log all the responses

var basil = require('basil');
var app = basil();

app.use(function (bundle) {
  if (bundle.response) {
    console.log(bundle.response.headers);
    console.log(bundle.response.body.toString());
  } 
});

app.listen(8000);

4. Always return a 404 code

var basil = require('basil');
var app = basil();

app.use(function (bundle) {
  if (bundle.response) {
    bundle.response.status = 404;
  } 
});

app.listen(8000);

1 Response
Add your response

Hi @dawicorti, I created something very similar called json-proxy. We should sync up efforts. Your post inspired me to think about an option to capture API responses to disk and allow you to replay them without needing the API server.

over 1 year ago ·