Light-http 是一個輕量級的Library ,讓你可以用很簡單、快速的方式建立一個 HTTP/HTTPS 的 Request,而且還同時支援同步與異步請求,同步的功能是使用 Promise Pattern ,透過 Promise 可以讓程式更容易維護。
不只是 Node.js (io.js) 可以使用 light-http ,light-Http 也支援一般常用的瀏覽器,例如 IE8 ~ , Chrome, Firefox。
使用 NPM 指令就可以馬上試用 light-http
npm install light-http
這裡提供一些簡單的 light-http 使用範例.
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 });
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 });
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 });
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); });