The JS runtimes
Published in

The JS runtimes

Deno vs Node.js: Native HTTP clients

This article is a part of the equivalent series that provide Deno’s equivalent of commonly used functionality provided by Node.js.

In this article, we’ll compare how native HTTP clients are used in Deno (using fetch) & Node.js (using http.request).

  • In Node.js, an import from core http module is required
  • In Deno, no import is required as http client is a part of the core runtime (except for streaming files)
  • In Node.js, HTTP request/response handling is through callbacks
  • In Deno, HTTP request/response handling is through promises
  • In Node.js, a basic HTTP request can be sent through the request API that returns the response through a callback function
  • In Deno, a basic HTTP request can be sent through the async fetch API
  • In Node.js, statusCode, statusMessage & headers contains the response data (not the body)
  • In Deno, status, statusText & headers contains the response data (not the body)
  • In Node.js, response headers can be accessed directly via dot notation
  • In Deno, response headers need to be accessed through get function
  • In Node.js, response body needs to be collected through ‘data’ callbacks (multiple callbacks are possible)
  • In Deno, response body is collected by the API (no work on user)
  • In Node.js, body can be sent through write API call (string or buffer)
  • In Deno, body can be sent through body attribute (string, form data, url search params, stream, blob, etc.)
  • In Node.js, JSON encoding/decoding needs to be done by the user
  • In Deno, JSON encoding needs to be done by the user while JSON decoding is through API
  • In Node.js, a file stream can be prepared & piped directly to the http request object
  • In Deno, a file stream needs to be prepared using standard library’s readableStreamFromReader function, then given to the body attribute
  • In Node.js, URLSearchParams can be used to create a URL encoded body & then converted to string for the write API
  • In Deno, URLSearchParams can also be used to create a URL encoded body & given directly to the body attribute

This story is a part of the exclusive medium publication on Deno: Deno World.

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store