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();
Related
I create a simple http server from which I want do transfer some bytes of data over socket. So i am listening to 'connect' event of the server. But it is never called?
here is my code.
var http = require('http');
var server = http.createServer(function(req, res) {
res.setHeader('Content-type', 'text/html');
res.end('<h3>Yeah! you are connected on ' + Date() + '</h3>');
console.log('User connected');
});
server.on('connect', function(req, socket, head) {
//var addr = socket.remoteAddress;
console.log('IP - ');
});
server.listen(8000);
For connect event, when the server is running, need to make a request to a tunneling proxy.
Replace your server.listen(8000); with this:
server.listen(8000, '127.0.0.1', () => {
// make a request to a tunneling proxy
const options = {
port: 8000,
hostname: '127.0.0.1',
method: 'CONNECT',
path: 'www.google.com:80'
};
const req = http.request(options);
req.end();
});
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
};
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..!
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);
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);
});