Code Monkey home page Code Monkey logo

basil's Introduction

Basil

Middleware oriented proxy

About

Basil is a simple but extendable proxy. It makes easy for developers to alter requests and responses. Basil has no such thing as configuration file or a command line program. Requests and Responses can be modified by inserting logical handlers.

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

What's the bundle object?

As you see, your functions are called for each requests and responses. Everytime you'll get a bundle object.

Here is its structure:

  • bundle.request: the options object you usually give to http.request
  • bundle.request.headers: An object model of the HTTP headers the client sent.
  • bundle.request.body: the Buffer object of the request body (normally in POST requests).

In case you are response side you'll get:

  • bundle.response.status: The status code that will be returned
  • bundle.response.headers: An object model of the HTTP headers the client will receive.
  • bundle.response.body: the Buffer object of the returned data.

In the bundle object, everything can be read/written.

basil's People

Contributors

avetisk avatar dawee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

avetisk

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.