I have 2 nodejs apps one running in port 8000 that only returns "hello"
and another app running on port 3000 that makes a simple http request to the first app
var http = require('http');
var r = http.get({
host: 'localhost',
path: '/',
port: '8000'
},
function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
console.log(body);
});
});
the console log returns
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:8000
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
What´s the problem here?
the first app is running correctly in http://localhost:8000/ but for some reason
when the second app makes a request to the first app I get the error I posted above. thanks for your help.
Seems like first app (on port 8000) is not reachable or not started at the moment, when second app sends request.
Related
I am trying to get a malformed HTTP response using node. Since the response is malformed, I cannot use request normally (it would give the HPE_INVALID_CONSTANT error)
From this answer https://stackoverflow.com/a/23543522/1748450 I can get the raw HTTP response using the net module like so:
var net = require('net');
var host = '192.168.1.1',
port = 80,
socket = net.connect(port, host, function() {
var request = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n",
rawResponse = "";
// send http request:
socket.end(request);
// assume utf-8 encoding:
socket.setEncoding('utf-8');
// collect raw http message:
socket.on('data', function(chunk) {
rawResponse += chunk;
});
socket.on('end', function(){
console.log(rawResponse);
});
});
However, this only works with getting the response from the host's root page (192.168.1.1). The page I'm trying to get the response from is actually 192.168.1.1/admin/landingpage.fwd.
If I try to edit host to that URL then I get this error:
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 192.168.1.1/admin/landingpage.fwd
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: '192.168.1.1/admin/landingpage.fwd'
}
Is this possible to fetch this using the net module in the above example?
If not possible, what other way can I use to get the raw HTTP response from that URL?
You can just put the path in your http request as in:
var request = "GET /admin/landingpage.fwd HTTP/1.1\r\nHost: " + host + "\r\n\r\n",?
That's where the path goes in an http request.
I did make a get request to an API but get an ECONNREFUSED Error. The problem is not the API because when I type it in a browser, I get back results in JSON.
This is my code;
var https = require("https");
var options = {
host : 'nairabox.com',
port : 443,
path : '/v1/tickets/auth=APIKEY&as=showtimes&cinemaId=CINEMAID&ticketId=TICKETID',
method : 'GET'
}
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log( JSON.parse(data));
});
});
req.end();
req.on('error', function(err){
console.log("Error: ", err);
});
This is the error;
Error: { Error: connect ECONNREFUSED 162.255.119.75:443
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '162.255.119.75',
port: 443 }
Anyone can be kind enough to test it replacing the parameters with random numbers and you'd get the same error. How can I fix it. API gives results in the browser though.
The hostname nairabox.com resolves to two IP-numbers, 178.79.175.127 and 162.255.119.75. The latter is refusing connections, which is the issue you're running in to.
However, the host www.nairabox.com resolves to only one IP-number, 178.79.175.127, so I guess you should be using that hostname instead of the one without the www. prefix.
I'm trying to run a basic node.js file on an aws server running ubuntu 14.04 and apache 2.4.7
var http = require('http');
var hostname = '33.33.33.33';
var port = 3000;
var server = http.createServer(function(req, res) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello World</h1>');
});
server.listen(port, hostname, function() {
console.log('Server running at http://${hostname}:${port}/');
});
The hostname is just the IP to the server. Should it be something else? Should the hostname be the IP or should it be something else?
The above code gives the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at net.js:1135:9
at dns.js:72:18
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:901:3
********Update*********
I have updated my code with localhost. That got rid of the error and allowed me to run the .js file. However I can't access the file from the server. I type in the IP like so
**.**.**.**:3000
This returns the message:
This site can’t be reached
**.**.**.** refused to connect.
ERR_CONNECTION_REFUSED
I also try accessing the location the file is located on the server but I get the same result.
**.**.**.**:3000/nodelearning/c1_node_week1/node-express
After I run:
node myNodeApp.js
In the terminal, I just need to access the IP of the server from a web browser right? Do I need to access only the root **.**.**.**:3000 or do I need to access the specific location of the node file **.**.**.**:3000/learningNode/myNodeApp.js
I only need to access the root right?
So **.**.**.**:3000 should work?
Below is the .js file that I'm able to run. But I can't access.
var express = require('express'),
http = require('http');
var hostname = 'localhost';
var port = 3000;
var app = express();
app.use(function (req,res, next) {
console.log(req.headers);
res.writeHead(200, {'Content-Type': 'text/html' });
res.end('<html><body><h1>Hello World</h1></body></html>');
});
var server = http.createServer(app);
server.listen(port, hostname, function(){
console.log('Server running at http:// NVM');
});
Cheers
the issue is with
var hostname = '33.33.33.33';
because when routes are recycled new ip address are assigned to the machine. so this will fail. As a recomendation skip host parameter in listen() or if you still want to use hostname use
var hostname = '127.0.0.1';
or
var hostname = 'localhost';
hope it helps :)
I need to send my client HTTPS requests through an intranet proxy to a server.
I use both https and request+global-tunnel and neither solutions seem to work.
The similar code with 'http' works. Is there other settings I missed?
The code failed with an error:
REQUEST:
problem with request: tunneling socket could not be established, cause=socket hang up
HTTPS:
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at SecurePair.error (tls.js:1011:23)
at EncryptedStream.CryptoStream._done (tls.js:703:22)
at CleartextStream.read [as _read] (tls.js:499:24)
The code is the simple https test.
var http = require("https");
var options = {
host: "proxy.myplace.com",
port: 912,
path: "https://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.
In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.
I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.
Example code using request client:
var request = require('request');
request({
url: 'https://www.google.com',
proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(response);
}
});
Example code using no external dependencies:
var http = require('http'),
tls = require('tls');
var req = http.request({
host: '97.77.104.22',
port: 3128,
method: 'CONNECT',
path: 'twitter.com:443'
});
req.on('connect', function (res, socket, head) {
var tlsConnection = tls.connect({
host: 'twitter.com',
socket: socket
}, function () {
tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
});
tlsConnection.on('data', function (data) {
console.log(data.toString());
});
});
req.end();
I have been trying to fix this for hours, with endless googling, I try to start the app, go to the url and see a 503 Service Unavailable error, I then cd into app-root/repo, try to manually start server.js, and get the following:
[my-app-url.rhcloud.com repo]\> node server.js
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Connecting to server
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at net.js:1143:9
at dns.js:72:18
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:902:3
This is driving me insane, all I'm trying to do is a simple api, and it works perfectly in my local environment.
Thank you.
Already another program or instance of this program is running on same port.
run - sudo netstat -tapen | grep ":<<your given port>>"
and then kill the process.
Then try to run the server...
Thanks
You need to bind to the OPENSHIFT_NODEJS_IP, i see you are only binding to the correct port, not the ip also: https://developers.openshift.com/en/node-js-getting-started.html
https://github.com/openshift-quickstart/openshift-nodejs-http-and-websocket-example/blob/master/server.js#L1
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var WebSocketServer = require('ws').Server
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write("Welcome to Node.js on OpenShift!\n\n");
response.end("Thanks for visiting us! \n");
});
server.listen( port, ipaddress, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wss = new WebSocketServer({
server: server,
autoAcceptConnections: false
});
wss.on('connection', function(ws) {
console.log("New connection");
ws.on('message', function(message) {
ws.send("Received: " + message);
});
ws.send('Welcome!');
});
console.log("Listening to " + ipaddress + ":" + port + "...");