AWS Lambda- get html of the website - node.js

I am trying to create function in AWS Lambda which returns html of the given website. That is my code:
console.log('Loading function');
exports.handler = (event, context, callback) => {
var util = require("util"),
http = require("http");
var options = {
host: "www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html",
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);
callback(null, content);
});
});
req.end();
};
It works perfectly for 'www.google.com' as a host parameter in options but when I try with more complicated, similar to the given one in the code, I get an error:
Error: getaddrinfo ENOTFOUND www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html:80
at errnoException (dns.js:26:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

If you look at the documentation for the http module, you'll see:
host: domain name or IP address of the server to issue the request to
path: request path, defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'

Related

Invoke api running on localhost from aws lambda in nodejs

I am running a api on http://localhost:80/api/test, I want to invoke this api from lambda function but I am not sure what plugin or anything I should use to access same.The reason for for doing so is, I want to do testing lambda and api in development phase
I used https://ngrok.com/ to solve it.
Below is the command for localhost https urls. You can replace the port no 3000 with your port no.
ngrok http https://localhost:3000 -host-header="localhost:3000"
Below is my Lambda Function:
var https = require('https');
exports.testJob = (event, context, callback) => {
var params = {
host: "90abcdef.ngrok.io",
path: "/api/test"
};
var req = https.request(params, function(res) {
let data = '';
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
console.log("DONE");
console.log(JSON.parse(data));
});
});
req.end();
};

Nodejs Event error Express

i have this code in express.
var express = require('express');
var http = require("http");
var https = require("https");
var app = express();
var optionsSB = {
host: 'domain.com',
path: '/wp-content/themes/domain/includes/ajax/get_properties.php'
};
var optionsLV = {
host: 'apiproperties.local',
path: '/properties/storeSB',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
https.get(optionsSB, function (https_res) {
var dataSB = "";
https_res.on("data", function (chunkSB) {
dataSB += chunkSB;
});
https_res.on("end", function () {
http.request(optionsLV, function(http_res){
var dataVL = "";
http_res.on("data", function (chunkVL) {
dataVL += chunkVL;
});
http_res.on("end", function () {
console.log(dataVL);
});
});
});
});
app.listen(3000, function () {});
I get this error
events.js:183
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
I already try some things but i dont know what is the problem, regards.
I follow some instruction from a tutorials and all works fine but that error i dont understand.
It will throw like this when you are getting an error in setting up the request with your https.get(), but you don't have any error handler to capture the error. You can supply an error handler:
https.get(...).on('error', function(err) {
// error here
console.log(err);
});
It appears that the specific error is ECONNREFUSED. It could be that the destination is not accepting your https connection or it could be that it doesn't like the way you were passing the options. Since all you have is a host and path, you can also just use the URL:
https.get("https://somedomain.com//wp-content/themes/domain/includes/ajax/get_properties.php", ...);

Error: getaddrinfo ENOTFOUND while making get request to localhost, Nodejs

I am trying to make a het request to etcd instance running in my local trough the node http module.
the code look like this
'use strict';
const express = require('express');
const app = express();
var http = require('http');
const port = 10111;
var encoded_url = encodeURI('/v2/keys/message -X GET');
var options = {
host: 'http://127.0.0.1:2379',
path: encoded_url
};
var callback = function (response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
app.listen(port, () => {
console.log("server started on port " + port);
});
but I am getting the following error
Error: getaddrinfo ENOTFOUND http://127.0.0.1:2379 http://127.0.0.1:2379:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
If I make the same curl request from the terminal I get the result
curl http://127.0.0.1:2379/v2/keys/message -X GET
not able to figure out what is the issue.
By default http.request() use port 80.
Use this instead:
var options = {
protocol: 'http:',
host: '127.0.0.1',
port: 2379,
path: encoded_url
};

Error: connect ECONNREFUSED. Unhandled 'error' event NodeJS

I am encountering the Nodejs error ECONNREFUSED 127.0.0.1:3000 when I try to send a get request to load a static file. I have seen many questions on this error but apparently there is no straight answer as to why this error is thrown. Here below is the code I have. I have tried changing localhost to 127.0.0.1 or change port numbers 3000, 7000, 8080 but nothing resolved it. Can someone please advised? Thank you.
//Basic web client retrieving content of file using get request
var http = require('http');
//options for request
var options = {
hostname: 'localhost',
port: '3000',
path: '../html/hello.html'
};
//function to handle response from request
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(response, error){
getResponse(response);
}).end();
Your client code is good enough to work. You are getting the ECONNREFUSED because there is a no server which is listening to the particular port number and the Server is not sending any data and you are requesting to get data from the server and accumulate it.
Here is the sample code:
//Requesting for http and fs modules
var http = require('http');
var fs = require('fs');
//creating the server and reading the file synchronously
var server = http.createServer(function(req, res) {
res.end(fs.readFileSync('./html1.html'));
}).listen(3000);
//creating the client which connects to local host at port number 3000
var options = {
hostname: 'localhost',
port: '3000',
}
//getting the data from server, storing it in to a variable, and printing at end of the data
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(response, error){
getResponse(response);
}).end();
the issue in your code , that you are not handling error, for this you have to handle error first.
var http = require('http');
var options = {
hostname: 'localhost',
port: '3000',
path: '../html/hello.html'
};
function getResponse(response){
var serverData='';
response.on('data', function(chunk){
serverData += chunk;
});
response.on('end', function(){
console.log(serverData);
});
};
http.request(options, function(error , response){
if(error){
console.log(error);//handle error here.
}else{
getResponse(response);
}}).end();

Handler response in nodejs

I am new in NodeJS, and I am working an an example:
function test(req,res){
var path = urls[Math.floor(Math.random()*urls.length)];
console.log("try to redirect to:"+path);
http.get(path,function(res_server){
//how to send the data from res_server to res
});
}
And the urls is an array of url.
I wonder how can I send the data from the res_server to the original client response?
BTW, the url maybe http or https.
update
var urls=["url1","url2","url3"];
var path = urls[Math.floor(Math.random()*urls.length)]; // find an random item from the array
update:2
Fine, this is the complete simple test script:
var http=require("http");
http.createServer(function(req, res1) {
var url = 'http://www.google.com.hk/images/srpr/logo11w.png';
var hp=require("http");
hp.get(url, function(res2) {
res2.pipe(res1);
});
}).listen(3000);
It works, but if you change http://www.google.com.hk/...logo..png to https:/www.google.....png
It will throw error:
http.js:1840
throw new Error('Protocol:' + options.protocol + ' not supported.');
^
Error: Protocol:https: not supported.
at Object.exports.request (http.js:1840:11)
at Object.exports.get (http.js:1847:21)
at Server.<anonymous> (C:\Users\maven\Desktop\t.js:6:6)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:525:27)
Change var http = require('http'); to var http = require('https');
I do not fully understand your example. Looks strange to me. However best would be to pipe the request response into the server response:
http.createServer(function(req, res1) {
var path = url.format({
protocol: 'http:',
host: 'www.google.com'
});
http.get(path, function(res2) {
res2.pipe(res1);
});
}).listen(3000);

Resources