Is there a URL in Nodejs?

Asked

Viewed 3,112 times

5

Good is the following I needed some function that served me as "Curl", to use in nodejs.

Is there any Function, which does the Curl equivalent in php?

Thank you.

2 answers

5


See the documentation for a full example and how to use the HTTP module: http://nodejs.org/docs/v0.5.2/api/http.html#http.request

There’s also this example

var http = require("http");

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

So yes, there is something equivalent.

  • 1

    I managed to use! Thanks for the help.

0

If libcurl-only functionality is required, there is a package that serves as binding for libcurl on Node.js called node-libcurl.

(I am the author of the same)

Example of use:

const { Curl } = require('node-libcurl');

const curl = new Curl();

curl.setOpt('URL', 'www.google.com');
curl.setOpt('FOLLOWLOCATION', true);

curl.on('end', function (statusCode, data, headers) {
  console.info(statusCode);
  console.info('---');
  console.info(data.length);
  console.info('---');
  console.info(this.getInfo( 'TOTAL_TIME'));

  this.close();
});

curl.on('error', function (error, errorCode) {
  // faça algo com error
  this.close()
});
curl.perform();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.