0.8.13 • Published 7 years ago

request-light.js v0.8.13

Weekly downloads
4
License
ISC
Repository
github
Last release
7 years ago

request-light.js | A Light-Weight Http Request library for node.js

Under Development Yet :)

A Light Http Request library for node.js. (Supporting gbk encoding. :)

References

Other http request library for node.js.

https://github.com/request/request

https://github.com/Mashape/unirest-nodejs

Attention:

A light http request library for NodeJS, supporting 'gbk/gb2312' encoding.

Why it is light, cause I do not believe that I can write something that are much complicated :)

It is a simple library drifted by someone amateur like me and please do not use it in the product level.


Get-Started

var Request = require('request');

Request
    .init({
        timeout: 6000,
        encoding: 'gb2312',
        host: 'google.com',
        method: 'GET',
        path: '/base-path/'
    });


Request
    //.get('remote-address')
    .post('remote-address')
    .config({
        timeout: 6000,
        encoding: 'gb2312'
    })
    .headers({
        'User-Agent': 'fisher95.com'
    })
    .send({
        someKey: 'some-value'
    })
    .done(function(err, res){
        if(err)
            return R.log(err);
        R.log(res.headers);
        R.log(res.body);
    });

Initialize Configure

Configure to initialize the library with global preferences of all your request by Request.config(configure);.

Request.config({
    debug: true,
    timeout: 6000,
    encoding: 'gb2312',
    host: 'google.com',
    method: 'GET',
    path: '/base-path/'
});

Configure to specify a single request by Request.post(remote-address).config(configure);

var url = 'http://google.com/';
Request.post(url)
    .query({
    	hello: 'world'
    })
    .config({
        timeout: 6000,
        encoding: 'gb2312',
        encodings: {
            request: 'gb2312',
            response: 'gb2312'
        }
    });

Configure retry when timeout globally. (Only retry when timeout)

Request.config({
    debug: true,
    timeout: 6000,
    retry: true,            // Whether retry (only) when timeout.
    retryMaxTime: 3000,     // Retry timeout is 3000.
    retryMaxTimes: 5        // At most  5  request will be sent.
});

Callback of request

var callback = function(err, response){
    console.log(res.code);
    console.log(res.status);
    console.log(res.message);
    console.log(res.length);
    console.log(res.headers);
    console.log(res.body);
}

Usage Sample

Download binary file.

Request.get( URL_BINARY_FILE )
    .config( {
        encoding: null  // set encoding as null to make the response body is instance of Buffer
    } )
    .done( function ( err, res ) {
        if ( err ) {return console.error( err );}
        fs.writeFile( FILE_DOWNLOADED, res.body, function ( err ) {
            if ( err ) {return console.error( err );}
            console.log( 'Saved file to: ' + FILE_DOWNLOADED );
        } )
    } );

Update Log

  • v0.8.6 2016-11-23
    • Added Support to handle multi-byte characters in the hard way. ref
    • Added response.length to represent the response body length.

0.8.13

7 years ago

0.8.12

7 years ago

0.8.11

7 years ago

0.8.10

7 years ago

0.8.9

7 years ago

0.8.8

7 years ago

0.8.7

7 years ago

0.8.6

7 years ago

0.8.5

7 years ago

0.8.4

8 years ago

0.8.3

8 years ago

0.8.2

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago