This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
README docs and others; for now, module is not named so examples are …
…using the `request` module
- Loading branch information
1 parent
83d7dc2
commit 9fae85b
Showing
6 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#### ***Note:** At this time, the module is not named, therefore, the name `request` is used to represent this module. | ||
| ||
# **Request** | ||
### A Node.js Request module, built using Promises with 0 dependencies. | ||
| ||
|
||
## **Usage** | ||
```js | ||
// require the request module | ||
const request = require('request') | ||
|
||
// supports promise chaining | ||
// use the built-in .json() function to return a JSON object | ||
request.get('http://ip.jsontest.com/?callback=showMyIP').json().then(console.log) | ||
//=> showMyIP({"ip": "127.0.0.1"}); | ||
|
||
// or use an asynchronous function to handle requests | ||
async function get(){ | ||
let response = await request.get('http://ip.jsontest.com/?callback=showMyIP').json() | ||
console.log(get()) | ||
//=> showMyIP({"ip": "127.0.0.1"}); | ||
} | ||
get() | ||
``` | ||
|
||
| ||
# **Docs** | ||
|
||
## Functions | ||
| ||
### **get**       `request.get(url, headers)` | ||
|
||
Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use `.buffer()`. | ||
|
||
    `.text()` | ||
    Returns the get response result as a string. | ||
        **Example:** | ||
> ```js | ||
> request.get('https://postman-echo.com/get').text().then(console.log) | ||
> ``` | ||
    `.json()` | ||
    Returns the response result as a JSON object. | ||
        **Example:** | ||
> ```js | ||
> request.get('https://postman-echo.com/get').json().then(console.log) | ||
> ``` | ||
    `.buffer()` | ||
    Returns the get response result as a buffer. | ||
        **Example:** | ||
> ```js | ||
> request.get('https://postman-echo.com/get').buffer().then(console.log) | ||
> ``` | ||
    `.error()` | ||
    Useful to check if a request returns an error without stopping the Node proccess, returns `undefined` otherwise. | ||
        **Example:** | ||
> ```js | ||
> console.log(request.get('https://postman-echo.com/get').error()) | ||
> //=> undefined | ||
> ``` | ||
| ||
### **post**       `request.post(url, headers)` | ||
Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use `.buffer()`. | ||
    `.text()` | ||
    Returns the post response result as a string. | ||
        **Example:** | ||
> ```js | ||
> request.post('https://postman-echo.com/post', { | ||
> data: JSON.stringify({ | ||
> user: 'request', | ||
> password: 'requestx@node', | ||
> email: 'requestx@npm.org' | ||
> }) | ||
>}).text().then(console.log) | ||
> ``` | ||
    `.json()` | ||
    Returns the response result as a JSON object. | ||
        **Example:** | ||
> ```js | ||
> request.post('https://postman-echo.com/post', { | ||
> data: JSON.stringify({ | ||
> user: 'request', | ||
> password: 'requestx@node', | ||
> email: 'requestx@npm.org' | ||
> }) | ||
>}).json().then(console.log) | ||
> ``` | ||
    `.buffer()` | ||
    Returns the get response result as a buffer. | ||
        **Example:** | ||
> ```js | ||
> request.post('https://postman-echo.com/post', { | ||
> data: JSON.stringify({ | ||
> user: 'request', | ||
> password: 'requestx@node', | ||
> email: 'requestx@npm.org' | ||
> }) | ||
>}).buffer().then(console.log) | ||
> ``` | ||
    `.error()` | ||
    Useful to check if a request returns an error without stopping the Node proccess, returns `undefined` otherwise. | ||
        **Example:** | ||
> ```js | ||
> console.log(request.post('https://postman-echo.com/post', { | ||
> data: JSON.stringify({ | ||
> user: 'request', | ||
> password: 'requestx@node', | ||
> email: 'requestx@npm.org' | ||
> }) | ||
>}).error()) | ||
> //=> undefined | ||
> ``` | ||
| ||
### **proxy**       `request.proxy(proxyUrl)` | ||
Proxies a request. Format the string like `protocol://proxyIp:proxyPort` (e.g. `http://127.0.0.1:3000`). Returns the type of request you want to make. Then, request the resource like normal. | ||
        **Example:** | ||
> ```js | ||
> request.proxy('http://127.0.0.1:80').get('https://wtfismyip.com/json').text().then(console.log) | ||
> ``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const request = require('../index') | ||
|
||
request.get('http://ip.jsontest.com/?callback=showMyIP').text().then(console.log) | ||
console.log(request.get('http://ip.jsontest.com/?callback=showMyIP').error()) | ||
|
||
//=> showMyIP({"ip": "127.0.0.1"}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters