https://github.com/dawee/basil
Middleware oriented proxy
https://github.com/dawee/basil
Last synced: 2 months ago
JSON representation
Middleware oriented proxy
- Host: GitHub
- URL: https://github.com/dawee/basil
- Owner: dawee
- Created: 2014-01-24T14:07:52.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-12T22:07:39.000Z (over 12 years ago)
- Last Synced: 2025-03-20T10:03:30.667Z (over 1 year ago)
- Language: JavaScript
- Size: 205 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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"
```js
var basil = require('basil');
var app = basil();
app.listen(8000);
```
### 2. Log all the requests
```js
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
```js
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
```js
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](http://nodejs.org/api/http.html#http_http_request_options_callback)
* **bundle.request.headers**: An object model of the HTTP headers the client sent.
* **bundle.request.body**: the [Buffer](http://nodejs.org/api/buffer.html) 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](http://nodejs.org/api/buffer.html) object of the returned data.
In the bundle object, **everything can be read/written.**