
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
node-xmlhttprequest
Advanced tools
A spec-compliant version of XMLHttpRequest that works in NodeJS.
npm i --save node-xmlhttprequest
import { XMLHttpRequest } from 'node-xmlhttprequest'
const xhr = new XMLHttpRequest()
xhr.open('GET', 'https://example.com')
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
const html = xhr.responseText
const statusCode = xhr.status
const statusText = xhr.statusText
}
}
xhr.send()
Synchronous XHR requests work too, without writing files as other packages do. Note that it does use worker threads.
Warning This option is only left in for compatibility reasons. It is not recommended for any reason as it will block the thread.
import { XMLHttpRequest } from 'node-xmlhttprequest'
const xhr = new XMLHttpRequest()
xhr.open('GET', 'https://example.com', false) // <-- note the false parameter
xhr.send()
assert(xhr.readyState === XMLHttpRequest.DONE)
const html = xhr.responseText
// ...
Note A relative URL is a URL that only contains a path.
Relative URLs do not behave the same as the browser. You must use undici to set a relative url, otherwise an error will be thrown.
import { XMLHttpRequest } from 'node-xmlhttprequest'
import { setGlobalOrigin } from 'undici'
setGlobalOrigin('https://example.com')
const xhr = new XMLHttpRequest()
xhr.open('GET', '/api/ping')
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
xhr.responseURL // https://example.com/api/ping
}
}
xhr.send()
FAQs
A spec-compliant version of XMLHttpRequest
The npm package node-xmlhttprequest receives a total of 34 weekly downloads. As such, node-xmlhttprequest popularity was classified as not popular.
We found that node-xmlhttprequest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.