Inside the code, I want to download "http://www.google.com" and store it in a string. I know how to do that in urllib in python. But how do you do it in Node.JS + Express?
Asked
Active
Viewed 6.4k times
33
-
Is there an easy way to do this? I'm hoping for a good set of "tools" that I can download as a module... – TIMEX Apr 27 '11 at 08:50
-
Can you explain what you mean by "inside the code"? – Anderson Green Oct 11 '12 at 20:18
4 Answers
33
var util = require("util"),
http = require("http");
var options = {
host: "www.google.com",
port: 80,
path: "/"
};
var content = "";
var req = http.request(options, function(res) {
res.setEncoding("utf8");
res.on("data", function (chunk) {
content += chunk;
});
res.on("end", function () {
util.log(content);
});
});
req.end();

yojimbo87
- 65,684
- 25
- 123
- 131
22
Using node.js you can just use the http.request method
http://nodejs.org/docs/v0.4.7/api/all.html#http.request
This method is built into node you just need to require http.
If you just want to do a GET, then you can use http.get
http://nodejs.org/docs/v0.4.7/api/all.html#http.get
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
(Example from node.js docs)
You could also use mikeal's request module

David
- 8,340
- 7
- 49
- 71
-
1The request module works best. I tried both and the native http module hangs on multiple/concurrent requests. – reubano Sep 24 '13 at 09:23
-
1That's true until you have to work with redirects, optional basic authorization etc. `npm install download` works best. – polkovnikov.ph Sep 08 '14 at 17:28
-
3
-
-
1request is deprecated now https://github.com/request/request/issues/3142 – Code Maniac May 05 '20 at 12:46
20
Simple short and efficient code :)
var request = require("request");
request(
{ uri: "http://www.sitepoint.com" },
function(error, response, body) {
console.log(body);
}
);
doc link : https://github.com/request/request

Community
- 1
- 1

Natesh bhat
- 12,274
- 10
- 84
- 125
-
-
-
1
-
2Here's a list of [alternative libraries to request](https://github.com/request/request/issues/3143). – Marco Lackovic Jul 21 '20 at 21:55
1
Yo can try with axios
var axios = require('axios');
axios.get("http://www.sitepoint.com", {
headers: {
Referer: 'http://www.sitepoint.com',
'X-Requested-With': 'XMLHttpRequest'
}
}).then(function (response) {
console.log(response.data);
});

Gaurav Mogha
- 370
- 3
- 9