jf-http-request
Simple wrapper for NodeJS HTTP request.
Options
Option | Type | Description |
---|---|---|
auth | string | Basic authentication i.e. user:password to compute an Authorization header. |
family | number | IP address family to use when resolving host and hostname. Valid values are 4 or 6. When unspecified, both IP v4 and v6 will be used. |
headers | object | An object containing request headers. |
host | string | A domain name or IP address of the server to issue the request to (default: localhost ). |
hostname | string | Alias for host . To support url.parse() , hostname is preferred over host . |
localAddress | string | Local interface to bind for network connections. |
method | string | A string specifying the HTTP request method (default: GET ). |
path | string | Request path (default: / ). Should include query string if any: /index.html?page=12 . An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future. |
port | number | Port of remote server (default: 80 ). |
protocol | string | Protocol to use (default: http: ). |
socketPath | string | Unix Domain Socket (use one of host:port or socketPath). |
timeout | number | A number specifying the socket timeout in milliseconds. This will set the timeout before the socket is connected. |
body | * | Content to send to server (default: undefined ). |
requestType | string | Type of result to return ('promise', 'events') or use a function for use the callback system (defaults: events ). |
url | string | A string specifying the URL for request and passed to url.parse . |
Response types
There are three types of responses:
- ok :
code >= 200 && code < 300 || code = 304
- fail :
code < 200 || (code >= 300 && code !== 304)
- error : Any request error (timeout, no host, etc).
Request types:
With parameter requestType
you can change value returned (default: promise
).
Using callbacks
const jfHttpRequest = ;//...
Using events
const jfHttpRequest = ; // events: EDP way ;
Using promises
const jfHttpRequest = ; { try const response = await ; console; // ok & fail catch error console; // error } ;