1.0.4 • Published 6 years ago

website-obj v1.0.4

Weekly downloads
-
License
GNU General Publi...
Repository
github
Last release
6 years ago

WebsiteObj

WebsiteObj is a Node.js module that adds Sync HTTP requests and simple Async HTTP requests so you don't have to deal with all the events and just a simple promise/callback.

# Installing website-obj
npm install website-obj

Recommended Node.js Version WebsiteObj doesn't works properly on older Node.js versions since the http module didn't receive the whole website sometimes and it received only the half of it. This doesn't happens on the latest version which is why is recommended to use the latest one. You can still try other versions of Node.js, but if it doesn't work, you already know why.

  • Tested on
    • Node.js 8.X
    • Node.js 13.1.0

Changelog

  • 1.0.4
    • Added encoding as an option
    • Increased maxBuffer in spawnSync for prevent JSON syntax errors
    • body is no longer converted to string unless specified by encoding
    • requestSync() now accepts Infinity, NaN and -Infinity as values in the options
  • 1.0.3
    • Added support for the options object on the functions
    • Added redirection support for the request functions
    • Updated README.md to contain the new info and better grammar (Thanks @FlameKat53)
    • Added Table of Contents to README.md
  • 1.0.2
    • requestSync() now returns a simplified version of response
    • Moved the body string to the response object
    • Removed specificArg.js and related since it was useless
    • Updated the code examples in README.md
  • 1.0.1
    • Fixed grammar in README.md (Thanks @LightSage)
    • Fixed a mistake in the require code example
  • 1.0.0
    • Created WebsiteObj

Table of Contents

  • Class: Website
    • new Website(URL)
    • Website.prototype.requestSync(options)
    • Website.prototype.request(options, callback)

Class: Website A Website object is responsible of requesting synchronously or asynchronously a website. It uses the built-in http and https modules in Node.js to procure it's requests and handle their events. Even though this object allows one to request synchronously a website, it is not recommended to use sync request on big websites, just on small websites so it doesn't pause the script execution for too long. To import the Website class, you need to require the module. You can use the following example code to do that

// Requiring website-obj
const Website = require('website-obj');

new Website(URL) Creates a new Website object.

  • URL \ The link of the website you are requesting. Make sure it starts with http:// or https://.
  • Returns: \ Object with the request functions.
const Web = new Website('https://nodejs.org/en/');

Website.prototype.requestSync(options) Requests a website synchronously.

  • options \ Optional Accepts the same options as http.get() and 3 more options - redirections \ If set to true, it'll follow the redirections of the requested website. Default: false - maxRedirections \ The max amount of redirections. Default: 0 - encoding \ The encoding used for body. Default: 'buffer'
  • Returns: \ Response from the requested website (Includes body property). This response object doesn't include Functions or Circulars.
// Sync request
let response = Web.requestSync();

Website.prototype.request(options, callback) Requests a website asynchronously.

  • options \ Optional Accepts the same options as requestSync(). The function callback can be the first argument if you don't use options
  • callback \ Optional If used, instead of returning a promise, this function will be called when the website is requested. - err \ The error that occurred during the request. - response \ Response from the requested website (Includes body property). This response object includes Functions and Circulars.
  • Returns: \ The promise gets the same arguments as callback.
// Using promises
Web.request()
 .then(response => {})
 .catch(err => console.error(err));

// Using callbacks
Web.request((err, response) => {
 if (err)
  console.error(err);
});

// Using await
let response = await Web.request();

Extra information

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago