Invoke api running on localhost from aws lambda in nodejs - node.js

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();
};

Related

A ECONNREFUSED error of the node. Js

My Node app is throwing a ECONNREFUSED error. The port should not be in use. Any ideas?
console.info('-----http-----');
console.info();
var http = require('http');
var options = {
hostname: 'localhost',
port: 6860,
path: '/',
method: 'post'
};
var req = http.request(options, function(res) {
console.log('status:' + res.statusCode);
res.setEncoding('UTF-8');
res.on('data', function(chunk) {
console.log('body:' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request:' + e.message);
});
req.end();
Seems you are trying to send post request to one of the URL (localhost),
the code you posted that alone will not work, somewhere your server should run i.,e localhost:6860
For that just you need to create a server which runs on the port 6860.
Execute this simple server.js file in separate terminal then run "localhost:6860" in your browser.
Then run your code in separate terminal, it will execute properly. Check your both terminals you will get the difference.
**server.js**
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
console.log(req.url)
console.log(req.method)
res.end('okay');
}).listen(6860, "localhost");
$node server.js
Hope it will help you..!

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();

Http request with IIS node

Http request does not seem to work as it should with iisnode ...
When I run this without running it under IIS work it just fine ...
Anyone know if I need to give any permission or something, or someone who has experience in general with http request to other domain using IIS Node.
I can unfortunately not show debug, since I have not gotten it to work real under IISNode.
var http = require('http');
var cached_defs = [];
function loadArticleDef() {
var options = {
host: 'http://someurl.com',
path: '/data/DataObjectDef/GetDef?ArticeID=mypage'
};
callback = function(response) {
response.on('data', function (chunk) {
cached_defs.push(JSON.parse(chunk));
});
response.on('end', function () {
console.log("end");
});
}
http.request(options, callback).end();
}

forward proxy in nodejs

i made a small forward proxy with nodejs and hosted it in appfog.
it's working in local after setting up the proxy of my browser, but when i try using the one hosted in appfog it's says:
*Errore 130 (net::ERR_PROXY_CONNECTION_FAILED): Connessione al server proxy non riuscita.*
this is my code:
var http = require('http');
http.createServer(function(request, response) {
http.get(request.url, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function(d) {
response.write(d);
});
res.on('end', function() {
response.end();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}).listen(8080);
Am i missing something?
your code is working but once i've tried using it like this:
var port = process.env.VCAP_APP_PORT || 8080;
var http = require('http');
var urldec = require('url');
http.createServer(function(request, response) {
var gotourl=urldec.parse(request.url);
var hosturl=gotourl.host;
var pathurl=gotourl.path;
var options = {
host: hosturl,
port: 80,
path: pathurl,
method: request.method
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function(d) {
response.write(d);
});
res.on('end', function() {
response.end();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
response.write("error");
response.end();
});
}).listen(port);
console.log(port);
It's still doesn't work: i got request timed out when i try to ping the address, i got the same ERR_PROXY_CONNECTION_FAILED... locally work but when i using the remote address as proxy it doesn't
First: The app needs to use the port number issued to it by cloudfoundry. The app sits behind a reverse proxy that takes incoming requests on port 80 and forwards them to the VCAP_APP_PORT.
var port = process.env.VCAP_APP_PORT || 8080; // 8080 only works on localhost
....
}).listen(port);
Then you access your hosted app like this:
http://<app_name>.<infra>.af.cm // port 80
And your local app:
http://localhost:8080
Second: You may need to use a options hash to send to the http.get method instead of just supplying the request.url.
var options = {
host: '<host part of url without the protocal prefix>',
path: '<path part of url>'
port: 80,
method: 'GET' }
I tested the following code on my local box and on AppFog and the IPs were different. Whatismyip returned my local interent ip address when running locally and on the AppFog hosted app it returned the AppFog server ip.
var port = process.env.VCAP_APP_PORT || 8080;
var http = require('http');
var options = {
host: "www.whatismyip.com",
port: 80,
path: '/',
method: 'GET'
};
http.createServer(function(request, response) {
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function(d) {
response.write(d);
});
res.on('end', function() {
response.end();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
response.write("error");
response.end();
});
}).listen(port);

Difficulties with node.js proxy

I am attempting to make a GET request for a single image on another server from node.js.
var http = require('http');
var site = http.createClient(80, '192.168.111.190');
var proxy_request = site.request('/image.png');
proxy_request.on('response', function (proxy_response) {
console.log('receiving response');
proxy_response.on('data', function (chunk) {
});
proxy_response.on('end', function () {
console.log('done');
});
});
And even with this code, I can't get the "receiving response" message to print out. Outside of node, I can do a curl http://192.168.111.190/image.png just fine, but is there something else I might be missing?
for get requests try the http.get API http://nodejs.org/docs/v0.4.9/api/http.html#http.get
var http = require('http');
var options = {
host: '192.168.111.190',
port: 80,
path: '/image.png'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

Resources