My Node http interface.
Use npm:
npm install git://github.com/shallker-wang/my-http.git
Or add to your package dependencies:
{
"dependencies": {
"my-http": "git://github.com/shallker-wang/my-http.git"
}
}
Require and create an instance of MyHttp
:
var MyHttp = require('my-http');
var http = new MyHttp();
var data = JSON.stringify({name: 'Curry Chicken'});
http.set('url', 'http://site.com/api/food/');
http.set('method', 'POST');
http.set('data', data);
http.setHeader('Content-Type', 'application/json;charset=utf-8');
http.setHeader('Content-Length', data.length);
http.on('error', function(err) {
console.log('Handling error');
});
http.on('complete', onComplete);
http.request(onComplete);
function onComplete(result, res) {
console.log('Done');
}
Set URL:
http.set('url', 'http://site.com/api/user');
Set method:
http.set('method', 'POST');
Set data:
http.set('data', 'Hello, World!');
Set a header:
http.setHeader('Content-Type', 'application/json');
Set event listeners:
http.on('error', function(err) {})
http.on('response', function(res) {})
http.on('complete', function(data) {})
Make a request:
http.request();
write a test