Phin

Node HTTP client

README

phin logo



The lightweight Node.js HTTP client




Simple Usage


  1. ``` js
  2. const p = require('phin')

  3. const res = await p('https://ethanent.me')

  4. console.log(res.body)
  5. ```

Note that the above should be in an async context! Phin also provides an unpromisified version of the library.


Install


  1. ```
  2. npm install phin
  3. ```


Why Phin?


Phin is relied upon by important projects and large companies. The hundreds of contributors at Less, for example, depend on Phin as part of their development process.

Also, Phin is very lightweight. To compare to other libraries, see Phin vs. the Competition.

Quick Demos


Simple POST:

  1. ``` js
  2. await p({
  3. url: 'https://ethanent.me',
  4. method: 'POST',
  5. data: {
  6.   hey: 'hi'
  7. }
  8. })
  9. ```

Unpromisified Usage


  1. ``` js
  2. const p = require('phin').unpromisified

  3. p('https://ethanent.me', (err, res) => {
  4. if (!err) console.log(res.body)
  5. })
  6. ```

Simple parsing of JSON:

  1. ``` js
  2. // (In async function in this case.)

  3. const res = await p({
  4. 'url': 'https://ethanent.me/name',
  5. 'parse': 'json'
  6. })

  7. console.log(res.body.first)
  8. ```

Default Options


  1. ``` js
  2. const ppostjson = p.defaults({
  3. 'method': 'POST',
  4. 'parse': 'json',
  5. 'timeout': 2000
  6. })

  7. // In async function...

  8. const res = await ppostjson('https://ethanent.me/somejson')
  9. // ^ An options object could also be used here to set other options.

  10. // Do things with res.body?
  11. ```

Custom Core HTTP Options


Phin allows you to set core HTTP options.

  1. ``` js
  2. await p({
  3. 'url': 'https://ethanent.me/name',
  4. 'core': {
  5.   'agent': myAgent // Assuming you'd already created myAgent earlier.
  6. }
  7. })
  8. ```


Full Documentation


There's a lot more which can be done with the Phin library.



Phin vs. the Competition


Phin is a very lightweight library, yet it contains all of the common HTTP client features included in competing libraries!

Here's a size comparison table:

Package | Size
request | request package size
superagent | superagent package size
got | got package size
axios | axios package size
isomorphic-fetch | isomorphic-fetch package size
r2 | r2 package size
node-fetch | node-fetch package size
phin | phin package size