node read url code example

Example 1: read data from url node js

var http = require('http');

var options = {
    host: 'google.com',
    path: '/'
}
var request = http.request(options, function (res) {
    var data = '';
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        console.log(data);

    });
});
request.on('error', function (e) {
    console.log(e.message);
});
request.end();

Example 2: how to get url node

const https = require("https"); //First require the module 

const url = https://url.com 
//good practice is to assign the url to a const named url//

app.get("/", function(req, res){
    https.get(url, function(res){
        console.log(res);//if you wish to console log the respone from the server
    });