Light HTTP

What is the Light HTTP?

Light-HTTP is a very light library. the purpose of this package is to easily make a http & https request, it also support asynchronous and synchonrous(promise) request at the same time. You can use this library on Node.js environment and any version of Browser such as IE8 ~, Chrome, Firefox.

Install the Light HTTP.

The following example will show you how to use light-http on Node.js environment.

npm install light-http


There are some examples here.

Asynchronous HTTP Request.

    var http = require('light-http');
    var header = {"user-agent": "Mozilla/5.0 xx"};
    var url = "https://www.google.com.tw";

    // Method GET
    http.get(url, {"key":"value"}, header, function(response) {
        xxx
    });

    // Method POST
    http.post(url, {"key":"value"}, header, function(response) {
        xxx
    });

Synchronous - Using Promise

    var http = require('light-http');
    var header = {"user-agent": "Mozilla/5.0 xx"};
    var url = "https://www.google.com.tw";

    // Method GET
    http.get(url, {"key":"value"}, header)
        .then(function(response) {
            xxx
        });

    // Method POST
    http.post(url, {"key":"value"}, header)
        .then(function(response) {
            xxx
        });

File Upload

    var http = require('light-http');
    var header = {"user-agent": "Mozilla/5.0 xx"};
    var url = "https://www.google.com.tw";

    http.addFile("fileData", "/www/var/file.txt");
    http.addFileContent("fileData", "file.txt", "content");

    http.post(url, {"key":"value"}, header)
        .then(function(response) {
            xxx
        });

How to make a raw HTTP request?

    var http = require('light-http');
    var host = "www.google.com.tw";
    var port = 80;
    var path = "/";
    var cookie = 'SID=; HSID=; SSID=jjj; APISID=;';
    var msg = [
    "GET " + path + " HTTP/1.1",
    "host: " + host,
    "cookie: " + cookie,
    "\r\n"].join("\r\n");

    http.rawRequest(host, port, msg)
        .then(function (resp) {
            console.log(resp);
        });