dasu: Consistent XHR API for Client and Server

0.4.3 · abandoned · verified Sun Apr 19

dasu is a JavaScript library that aimed to provide a consistent XMLHttpRequest (XHR) API for both client-side (browser) and server-side (Node.js) environments. It abstracts away the underlying differences between `window.XMLHttpRequest` and Node's `http.request`, presenting a unified interface. The library, currently at version 0.4.3, was last published in June 2014. Its primary differentiator was simplifying basic HTTP requests with a single API across environments, which was particularly useful before the widespread adoption of Fetch API or more mature cross-platform libraries. Due to its age and lack of updates, it does not support modern JavaScript features like Promises or `async/await` and relies exclusively on CommonJS modules.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a GET request using dasu's `req` function with Node.js-style options and a callback.

const dasu = require('dasu');
const req = dasu.req;

// Same parameters as Node's require('http').request
const params = {
  method: 'GET',
  protocol: 'http',
  hostname: 'uinames.com',
  port: 80,
  path: '/api/',
};

req(params, function (err, res, data) {
  if (err) {
    console.error('Request failed:', err.message);
    return;
  }
  console.log('Status Code:', res.statusCode);
  console.log('Headers:', res.headers);
  try {
    const json = JSON.parse(data);
    console.log('Response Data:', json);
  } catch (parseError) {
    console.error('Failed to parse JSON:', parseError.message);
    console.log('Raw Data:', data.toString());
  }
});

// Optionally, disable auto-follow redirects
dasu.follow = false;

// Optionally, force mode 'node', 'browser', 'auto'
dasu.mode = 'auto'; // Uses window.XMLHttpRequest if available

view raw JSON →