website-obj v1.0.4
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
inspawnSync
for prevent JSON syntax errors body
is no longer converted to string unless specified byencoding
requestSync()
now acceptsInfinity
,NaN
and-Infinity
as values in the options
- Added
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
- Added support for the
1.0.2
requestSync()
now returns a simplified version ofresponse
- Moved the
body
string to theresponse
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
- Fixed grammar in
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 withhttp://
orhttps://
.- 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 totrue
, it'll follow the redirections of the requested website. Default:false
-maxRedirections
\ The max amount of redirections. Default:0
-encoding
\ The encoding used forbody
. Default:'buffer'
- Returns: \
Response from the requested website (Includes
body
property). Thisresponse
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 functioncallback
can be the first argument if you don't useoptions
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 (Includesbody
property). Thisresponse
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
- Discord
- @Javier107#6900
- Github
- NPM