Node.js 'socket hang up' and keep-alive - node.js

I have a very strange error happening.
I am running Node.js version 8.0.0 on Mac OS X Sierra.
I create a very simple http server that basically returns 'ok' to any request after 8 seconds in this example.
I then use the 'request' package to make a request to my http server every 9 seconds, and use the parameter 'forever' to keep the http session alive.
One request out of two works perfectly fine while the other out of two returns a 'socket hang up' error.
Here is the very simple code to reproduce the issue:
const http = require('http');
const request = require('request');
const port = 3000;
const server = http.createServer((req, res) => {
console.log('request');
setTimeout(() => {
res.end('ok');
}, 8000);
});
server.listen(port);
setInterval(() => {
request(`http://localhost:${port}/`, {forever: true}, (error, response, body) => {
if (error) return console.log('error', error);
console.log('done', body);
});
}, 9000);
When I run this code I get the following output on my Macbook Pro :
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
So I get an 'socket hang up' error exactly every other request.
It seems that the error does not seem to appear on an Ubuntu VPS though.
Any ideas as to why this issue is happening ?

Related

EPROTO when trying to sock.end in Node.js

I am trying to create a simple node server and call it. Here is what I am doing:
var net = require('net');
const https = require('https');
var srv = net.createServer(function(sock) {
sock.end('Hello world\n');
});
srv.listen(0, function() {
console.log('Listening on port ' + srv.address().port);
https.get('https://localhost:' + srv.address().port);
});
Here is the error I am getting:
Listening on port 35147
events.js:167
throw er; // Unhandled 'error' event
^
Error: write EPROTO 140575534664576:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:252:
at WriteWrap.afterWrite [as oncomplete] (net.js:833:14)
Emitted 'error' event at:
at TLSSocket.socketErrorListener (_http_client.js:382:9)
at TLSSocket.emit (events.js:182:13)
at onwriteError (_stream_writable.js:431:12)
at onwrite (_stream_writable.js:456:5)
at _destroy (internal/streams/destroy.js:40:7)at TLSSocket.Socket._destroy (net.js:603:3)
at TLSSocket.destroy (internal/streams/destroy.js:32:8)
at WriteWrap.afterWrite [as oncomplete] (net.js:835:10)
I am trying to run that code here: https://www.tutorialspoint.com/execute_nodejs_online.php.
How could I make the sock.end or is there an easier way to create a node server listener? I.e. without using the sockets.

nodejs http request socket hang up

I've been trying to send http reqeust by http node module by following code
const https = require('https')
http.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
but got the message
events.js:174
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:332:15)
at TLSSocket.socketOnEnd (_http_client.js:435:23)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at TLSSocket.socketOnEnd (_http_client.js:435:9)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
When I switch the code to
const request = require('request')
request.get('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
it worked and got a response with 200.
Then I try to capture network packages by Fiddler, I found no package was sent when using http.request.
This error only occurs in Windows system inside our working domain network.
maybe try https instead of http since you defined it https
https.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})

Node JS connection establishment between two server

In my sample project, I have used Google recaptcha. I need to verify captcha response at back end (Node JS). In NodeJS, I have used request module to connect with google server. But, I got some error like following
Error: connect ECONNREFUSED 172.217.166.100:443
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
Node JS
formData = {
secret: 'xxxxxx',
response: 'yyyyyy'
}
request.post({
url: "https://www.google.com/recaptcha/api/siteverify",
form: formData
},
function (err, httpResponse, body) {
if (err) throw err;
if (body) {
res.send(body);
}
});
When I am trying to set proxy to this above NodeJS code like following...
formData = {
secret: 'xxxxxx',
response: 'yyyyyy'
}
request.post({
url: "https://www.google.com/recaptcha/api/siteverify",
form: formData,
proxy: '172.217.166.100' /// Google server IP
},
function (err, httpResponse, body) {
if (err) throw err;
if (body) {
res.send(body);
}
});
I got these errors....
Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:80
at ClientRequest.onError (C:\Users\Desktop\project\Backend\node_modules\tunnel-agent\index.js:177:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Can anyone help me to resolve this problem
Thanks in advance
Looks like you've specified something like,
proxy: '172.217.166.100' /// Google server IP
which is wrong, as you can't determine the IP address of google since it is dynamic and often changes it.
You can obtain a latest proxy IP from the below link,
https://code.google.com/archive/p/recaptcha/wikis/FirewallsAndRecaptcha.wiki
The latest available IP's are,
ip4:216.239.32.0/19 ip4:64.233.160.0/19 ip4:66.249.80.0/20
ip4:72.14.192.0/18 ip4:209.85.128.0/17 ip4:66.102.0.0/20
ip4:74.125.0.0/16 ip4:64.18.0.0/20 ip4:207.126.144.0/20
ip4:173.194.0.0/16
Hope this helps!

Can't set headers after they are sent when trying to redirect after using Google's oauth2Client.getToken()

After receiving oauth2 tokens from google I am trying to redirect to an endpoint on my express server to make api requests.
This is the flow of the auth requests:
app.get('/api/presentation/getpresentation/:id', function(req, res, next){
req.session.user = req.user.id;
req.session.presentation_id = req.params.id;
res.redirect(google_auth_url)
});
app.get('/oauth2callback', function(req, res) {
const code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
req.session.tokens = tokens;
if (!err) {
oauth2Client.setCredentials(tokens);
res.redirect(`/api/presentation/${req.session.presentation_id}`);
return;
}
res.status(500).send(err);
})
});
app.get('/api/presentation/:id', **do things with api consent**);
The error:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:767:10)
at ServerResponse.location (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:884:15)
at ServerResponse.redirect (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:922:18)
at /Users/joe/school/solo-project/present/server/index.js:112:11
at /Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/auth/oauth2client.js:154:5
at Request._callback (/Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/transporters.js:106:7)
at Request.self.callback (/Users/joe/school/solo-project/present/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1057:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Any help is greatly appreciated, I've been tearing my hair out on this one for a while!
Thanks!

Node.js csv-parse error mystery using Sails

It processes most of the records from the .csv file. However, at the end I get this error with csv-parse node module and can't figure out why. It terminates my server so I need to resolve it. Please help
events.js:160
throw er; // Unhandled 'error' event
^
Error: Number of columns on line 37052 does not match header
at Error (native)
at Parser.__push (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:222:13)
at Parser._flush (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:189:12)
at Parser.<anonymous> (_stream_transform.js:118:12)
at Parser.g (events.js:291:16)
at emitNone (events.js:86:13)
at Parser.emit (events.js:185:7)
at prefinish (_stream_writable.js:504:12)
at finishMaybe (_stream_writable.js:512:7)
at endWritable (_stream_writable.js:524:3)
at Parser.Writable.end (_stream_writable.js:489:5)
at ReadStream.onend (_stream_readable.js:511:10)
at ReadStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at ReadStream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
My Controller API
upload: function(req, res){
req.file('statData').upload(function(error, files){
var fs = require('fs');
var parse = require('csv-parse');
var csvData=[];
var lines = 0;
var filePath = sails.config.appPath + '/data/file.csv';
fs.createReadStream(filePath)
.pipe(parse({
columns: false,
delimiter: '\t',
skip_empty_lines: true,
relax: true
}))
.on('data', function(csvrow) {
csvData.push(csvrow);
})
.on('end',function() {
//do something wiht csvData
});
}
I switched to using fast-csv module and worked.

Resources