DEV Community

Cover image for Poke: A Super Handy HTTP Request Library
Lawson Cheng
Lawson Cheng

Posted on

Poke: A Super Handy HTTP Request Library

'Request/request' was my favorite library all the time when i need to make http request in node, however it's deprecated for some time.

I have tried a lot of alternatives but none of them gives me the similar experience as request does.

Finally, I decided to make one, that will be my pleasure to have you guy's feedback : )

https://github.com/LawsonCheng/poke

import poke from 'js.poke'

// Using promise
poke('https://httpbin.org/anything')
.promise()
.then(result => {
    // response body here
    console.log(result.body)
    // get result in json format
    return result.json()
})
.then(json => {
    // here is the json object
    console.log('json: ', json)
})
.catch(err => {
    console.log('> Error: ', err)
})
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
anshrk profile image
Anshrk • Edited

Looks cool, but how is it different from fetch or axios?

Collapse
 
lawson_cheng profile image
Lawson Cheng

The poke library also support listen to difference events,
and easy access to stream : )

E.g

// Using callback
poke(hostname , pokeOptions)
// listen to response retrived
.on('response', result => {
    console.log(result)
})
// on chunk is recieved
.on('data', (chunk) => {
    // handle your data here
    console.log(d)
})
// on request eneded
.on('end', () => {
    console.log('Request is finished')
})
// listening to error
.on('error', (result) => {
    // handle error
    console.log(result.error)
})
Enter fullscreen mode Exit fullscreen mode

e.g 2

// get image
poke('https://via.placeholder.com/100x100')
// write data as an image file
.pipe(fs.createWriteStream('image.png'))
Enter fullscreen mode Exit fullscreen mode