showing results for - "how to run curl in javascript"
Julian
17 Sep 2016
1#Since javascript already has functions for this purpose
2#so it is not advisable to load an extra library for that
3#you can achieve the same in native javascript like this
4var url = "http://www.google.com";
5
6var xhr = new XMLHttpRequest();
7xhr.open("GET", url);
8
9xhr.onreadystatechange = function () {
10   if (xhr.readyState === 4) {
11      console.log(xhr.status);
12      console.log(xhr.responseText);
13   }};
14
15xhr.send();
16
17#But still if you want to do it using curl in Node.js, it is possible
18#Take a look at: https://github.com/JCMais/node-libcurl
19var Curl = require( 'node-libcurl' ).Curl;
20
21var curl = new Curl();
22
23curl.setOpt( 'URL', 'http://www.google.com' );
24curl.setOpt( 'FOLLOWLOCATION', true );
25
26curl.on( 'end', function( statusCode, body, headers ) {
27
28    console.info( statusCode );
29    console.info( '---' );
30    console.info( body.length );
31    console.info( '---' );
32    console.info( this.getInfo( 'TOTAL_TIME' ) );
33
34    this.close();
35});
36
37curl.on( 'error', function ( err, errCode ) {
38
39    //do something
40
41    this.close();
42});
43
44curl.perform();
45
46
similar questions
curl node exporter