Arrow keys: ← previous · → next 9 of 35
@nodedk/inet
0.2.2 stable

inet#

Download files and request JSON over HTTP or HTTPS.

Install#

npm i @nodedk/inet

Usage#

var inet = require('@nodedk/inet')

async function main() {
  var result = await inet.download('https://example.com/file.txt', 'file.txt', {
    progress: function ({ count, total }) {
      console.log(count, total)
    }
  })
  console.log(result)

  var data = await inet.json('https://example.com/data.json')
  console.log(data)
}

main().catch(console.error)

API#

download(url, file, options = {})#

Downloads url into the destination file, replacing existing content. The parent directory must exist. Resolves to { count, total } in bytes; total is 0 when content length is unknown.

options.progress({ count, total }) runs at most once per 500 ms while streaming and once at completion. Other options are ignored. Follows at most five HTTP redirects. Non-2xx status, request, and stream errors reject the promise.

json(url, options = {})#

Resolves to the parsed response body. url is a string; options is passed to Node's HTTP or HTTPS request method, including method and headers. options.body is sent directly as a string or Buffer. The default method is GET.

Non-2xx status, network errors, and invalid JSON reject. Redirects are not followed.

var { json } = require('@nodedk/inet')

async function main() {
  var result = await json('https://example.com/items', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ name: 'Example' })
  })
  console.log(result)
}

main().catch(console.error)

Created by Vidar Eldøy