node-http-xhr
An implementation of XMLHttpRequest
for node.js
using
the http.request
API.
Motivation
This package was written to provide the XMLHttpRequest
API to test browser
code that is being tested in a node.js
environment.
Installation
npm install --save node-http-xhr
Usage
// Standalone usagevar XMLHttpRequest = ; // Usage as global XHR constructorglobalXMLHttpRequest = ; var req = ; // Event handlers via .on${event} properties:req { console;}; // or using .addEventListener(event, handler):req; req;req;
Note about browser environments
If you use a bundler like browserify
or webpack
that follows the browser
field in package.json
, the module will simply export window.XMLHttpRequest
.
This is provided for sake of compatibility.
Development
Documentation
To generate documentation:
npm run doc
The html
documentation will be placed in doc/
.
Tests
npm test
Known issues
Currently, some features are lacking:
- Some ProgressAPI events (
loadstart
,loadend
,progress
) responseType
values other than''
or'text'
and corresponding parsing- As a result of the above,
overrideMimeType()
isn't very useful
- As a result of the above,
setRequestHeader()
doesn't check for forbidden headers.withCredentials
is defined as an instance property, but doesn't do anything since there's no use case for CORS-like requests innode.js
right now.