Node app upstream prematurely closed connection while reading response - javascript

There are manu questions/answer regarding this but my issue not fixed from any solution there so I am writing here .
My node application is working fine on my dev environment but on prod when I request some api which take long time it throw 502 error .
On checking log I got this issue
2019/02/22 12:16:57 [error] 2575#0: *2 upstream prematurely closed
connection while reading response header from upstream, client:
172.31.85.97, server: , request: "GET /api//m2/v1/metrics_report?auth=asdsdffk HTTP/1.1", upstream:
"http://172.17.0.3:80/api/m2/v1/metrics_report?auth=asdsdffk", host:
"", referrer: ""

I think the error from nginx is indicating that connection is closed from server. Something must have broken in express application during the large data processing which has caused this. You can try to increase the timeouts in nginx to debug this properly -
location / {
proxy_read_timeout 1000s;
proxy_connect_timeout 1000s;
....
}

In my case, this was express misconfiguration. So, after reading and looking up here and there, I finally come up with the answer. Set the httpServer timeout just like the example below:
var httpServer = app.listen(8000, () =>
console.log('App is listening on port 8000.')
)
httpServer.setTimeout(1000)

Related

Failed to connect to localhost port 8000: Connection refused in Node.js

I'm trying to connect to the HTTP server in node.js.
I've spent my whole day searching online to solve my issue but unfortunately, it is of no use.
This is a simple code I wrote taking reference from official Node.js documentation.
Index.js
const http = require('http');
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(8080);
The code I am running inside CMD terminal
curl localhost:8080
This returns the following error:
I know this questions have been asked a thousands time in stack overflow but none fixed my issue. Anyone can help me?
In you code you are listening to port 8080 but from commandline you are trying 8000. Make sure you are using correct port and ensure its not in use.
You are trying to execute C:\Users\prabi\app\server.js when the real file is C:\Users\prabi\app\index.js so verify the start script in package.json and fix it.
Fire up another terminal and run the command:
node index.js
Then go back to your previous terminal window/tab and run curl localhost:8080 again. Should work.
The problem is that your code has been written well, but you aren't executing it at all. So if you don't run your node code, your http server cannot listen at the 8080 port in the first place.

There are always came out an error about the websocket when init a project by using reactjs or vuejs

When I create a project by using create-react-app or vue init, and execute the command npm run dev/start, it always happened with an error about Websocket on my console tab. I don't understand what the problem is.
And the network tab always pending a request about socket constantly.
WebSocket connection to 'ws://localhost:3000/sockjs-node/863/gwfp1dnj/websocket' failed: Connection closed before receiving a handshake response
// these devServer options should be customized in /config/index.js
devServer:{
...
host: HOST || config.dev.host,
port: PORT || config.dev.port,
...
}
Ensure your dev host and port is configured correctly that like above.

Express.js piping request to php built-in server on localhost results in ECONNREFUSED

I am building a simple website using npm for development, and it is hosted with a provider with php support.
The only functionality that uses php is contact form to send email. the rest is simple html and javascript.
I use a simple php server in development started with php -S localhost:8000 to test a simple php email script and again in dev I reverse proxy requests for email.php to this php server locally.
Node app is on port 3000 and php server is on port 8000. The problem is I get connection refused error with the following express server configuration when request goes through localhost:3000/email.php:
#!/usr/bin/env node
var express = require('express');
var app = express(),
request= require('request'),
port = +(process.env.PORT || 3000);
app.set('case sensitive routing', false);
app.post( '/email.php', function( req, res ){
req.pipe( request({
url: 'http://localhost:8000/email.php',
qs: req.query,
method: req.method
}, function(error){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
// other request handlers here
app.listen(port, function() {
console.log('listening');
});
Php server is definitely up and serving all the pages on port 8000, which I can browse with the browser. I test it with curl and it seems to be handling the request just fine when posted directly to localhost:8000 using curl.
Not sure why I get this error, scratching my head, can't think of any reason.
Help is much appreciated.
I figured out what it was, d'oh! Well I am gonna post the answer in case someone else stumbles upon this.
PHP is to blame it seems; Checking the sockets listening a port using ss -ltn ( I am on Linux, this might not work for you) I realised php server is listening IPv6 only. Relevant output as follows:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 ::1:8000
With the relevant search I found the answer on web server documentation page under user notes posted by a user. See the post here. The solution is to use 127.0.0.1 rather than localhost:
As it turned out, if you started the php server with "php -S
localhost:80" the server will be started with ipv6 support only!
To access it via ipv4, you need to change the start up command like
so: "php -S 127.0.0.1:80" which starts server in ipv4 mode only.

EADDRINUSE error when re-running Node.js server on Windows 10

I'm having an issue where reloading (ctrl + c and then running again with node app) a basic Node.js server (tried v0.12.0 and v0.12.7) on Windows 10 causes the server to hang for about 20 seconds before reloading. When using a tool like PM2 to watch my server, it errors out after any update to the file and the log shows an EADDRINUSE error.
I boiled this down to the most simple possible example (thinking it might be an issue with Express) but it's still happening with the basic example from the Node.js documentation:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3030);
console.log('Server running at http://127.0.0.1:3030/');
I've checked that nothing else is running on port 3030. I've also tried different port numbers. It just seems that the Node.js process is not shutting down and releasing the port in a timely manner. I've also tried using ctrl+break, and also capturing the SIGINT and manually calling process.exit(). I can confirm that it catches the signal, but it still hangs as described.
I first noticed this shortly after upgrading to Windows 10 from 7, and was not having issues before, so I would think this is the most likely culprit. I have a dual-boot with Ubuntu 14.04, and I am having no issues there with the same code.
I'm no longer having the problem I described: it was likely something very specific to my setup since it wasn't reported anywhere else. I have since updated to Node 4.2.1 and installed various Windows updates, so hard to say what the original cause was.

HTTP Server on Android via node.js errors upon request

I'm running node.js v0.11 on Android (via https://github.com/paddybyers/node). I attempted to try out the "Hello HTTP" example found here: http://howtonode.org/hello-node, however, I ran into problems.
The server starts fine, but as soon as I attempt to connect to the http server (by visiting http://localhost:8000/), I get this error:
net.js:1156
COUNTER_NET_SERVER_CONNECTION(socket);
^
ReferenceError: COUNTER_NET_SERVER_CONNECTION is not defined
at TCP.onconnection (net.js:1156:3)
My code is exactly the same as the Hello HTTP example:
//Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
How can I fix this?
Thanks!
Compile Node.js with the HAVE_PERFCTR option. (You Need to implement your own perfctrs for Android. See node_counters.cc line 130).
It should be also possible to define the missing functions in your script as empty functions.
PS.:
You also need DTRACE.
I simply commented out all COUNTER_NET_, COUNTER_HTTP_, DTRACE_NET_, and DTRACE_HTTP_ calls in the javascript files under lib/. This amounted to about 10 or so lines that I commented out of net.js and http.js.
I think that js2c.py is supposed to process src/macros.py and src/perfctr_macros.py to effectively do this 'commenting out' for you when it serializes the scripts under lib/ to C arrays in out/release/src/node_natives.h, but that doesn't seem to be happening.

Categories

Resources