How to use XHR API in Node.js?
Asked Answered
D

4

32

This is a follow-up to my previous question

Suppose I've some javascript code, which runs fine on client (in a browser). This code makes a lot of XHR calls using the browser API.

Now I would like to run this code in node.js. Does node.js provide the XHR API as in browser ?

Diffractive answered 29/12, 2015 at 15:47 Comment(5)
On the server you can simply read the file using fs.readFile.Embrocate
You can try to create XHR Api using fs.readFileEmbrocate
fs.readFile() has nothing to do with XHR. If you want an XHR shim for node, there are several modules on npm that do that. However you should be aware that the entire API may not be the same, for example there would be no synchronous API because node only has async network I/O.Fenella
fs has asbolutely nothing to do with making an HTTP request, and despite what some may think, you absolutely do not need to install the "request" module--Node already has an API for this: nodejs.org/api/http.html#http_http_get_options_callbackStrouse
@Fenella most ajax request (unless it's CORS) get files from the same server so you can get them using fs.readFile(). You don't need http request to get those files.Embrocate
S
20

Natively Node.js does not provide the browser XHR API. There is, however, a node module xmlhttprequest that does.

If the file is on the server itself, you can use the fs.readFile or fs.readFileSync.

If it is on a remote server, then you can do an asynchronous XHR type request using a module like request: https://www.npmjs.com/package/request. This requires some rewriting of code.

Probably the least re-writing of your client-side code will be if you use the xmlhttprequest node module. It implements the browser XHR API for node.

Scorch answered 29/12, 2015 at 18:2 Comment(0)
E
22

You don't really need an XHR, since you can use http.request that comes natively with NodeJS, with it you can send GET, POST and PUT requests with headers and body.

Here is the link to the documentation http.request.

Epigoni answered 4/10, 2017 at 18:13 Comment(0)
S
20

Natively Node.js does not provide the browser XHR API. There is, however, a node module xmlhttprequest that does.

If the file is on the server itself, you can use the fs.readFile or fs.readFileSync.

If it is on a remote server, then you can do an asynchronous XHR type request using a module like request: https://www.npmjs.com/package/request. This requires some rewriting of code.

Probably the least re-writing of your client-side code will be if you use the xmlhttprequest node module. It implements the browser XHR API for node.

Scorch answered 29/12, 2015 at 18:2 Comment(0)
G
1

this is why we use Axios for modules. It can be used on client or server side without any dependency or code changes.

Gerry answered 14/4, 2022 at 16:57 Comment(0)
N
1

node-XMLHttpRequest

example code in nodejs

const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest

/* --- */

let xhr = new XMLHttpRequest()

xhr.open( 'GET',  'https://example.com' )

xhr.onload = function() {
    
    if ( xhr.status == 200 ) {
        
        console.log( xhr.responseText )
        
    } else {
        
        console.log( `Error: ${xhr.status}` )
        
    }
}

xhr.send()

replit

replit callback

replit promise

replit Async/Await

Nitin answered 2/12, 2022 at 1:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.