Monday 8 May 2017

Unexpected 'close' event in Node.js

I'm running a simple TLS Node.js server and testing with a client. I get unexpected close events on the server that screws up and ends my client connection.

Here is the link to TLS Node.js close event.

Here's my TLS server

var tls = require('tls');
var fs = require('fs');

var options = {
    key: fs.readFileSync('./keys/private-key.pem'),
    cert: fs.readFileSync('./keys/public-cert.pem')
};

var PORT = '20000';

console.log("Starting Server at Port: ", PORT);

tls.createServer(options, function (socket) {
   socket.on('data', function(data) {
   console.log(data + "\n");  
});

socket.on('close', function() {
    console.log("Client closed connection\n");
    // This is being called unexpectedly. 
});

socket.on('error', function(e) {
    //socket.destroy();
    console.log("ERROR, Client closed connection\n");
});

}).listen(PORT);

Command to run my client openssl s_client -connect localhost:20000

I get this message Client closed connection repeatedly.

I have some other modules installed and I'm wondering if its interfering. Here's my package.json

{
  "name": "mem",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "@angular/common": "2.3.0",
    "@angular/compiler": "2.3.0",
    "@angular/core": "2.3.0",
    "@angular/forms": "2.3.0",
    "@angular/http": "2.3.0",
    "@angular/platform-browser": "2.3.0",
    "@angular/platform-browser-dynamic": "2.3.0",
    "@angular/router": "3.3.0",
    "@angular/upgrade": "2.3.0",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.15",
    "body-parser": "~1.13.2",
    "compression": "^1.5.2",
    "connect": "^3.4.0",
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.3.5",
    "debug": "~2.2.0",
    "ejs": "^2.3.4",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "express": "~4.13.1",
    "express-cache-response-directive": "~1.0.0",
    "express-session": "^1.11.3",
    "fs": "0.0.2",
    "gulp": "^3.9.0",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-typescript": "^2.10.0",
    "gulp-webserver": "^0.9.1",
    "highcharts-ng": "0.0.11",
    "http": "0.0.0",
    "jade": "~1.11.0",
    "mongojs": "^2.4.0",
    "morgan": "^1.6.1",
    "mysql": "^2.9.0",
    "net": "^1.0.2",
    "nodemon": "^1.7.1",
    "passport": "^0.3.0",
    "passport-google-oauth": "^0.2.0",
    "path": "~0.12.7",
    "q": "~1.4.1",
    "reflect-metadata": "0.1.2",
    "request": "^2.79.0",
    "rxjs": "5.0.0-beta.6",
    "serve-favicon": "~2.3.0",
    "serve-static": "^1.10.0",
    "simple-text-parser": "~0.1.2",
    "systemjs": "0.19.6",
    "underscore": "^1.8.3",
    "zone.js": "^0.7.4"
  }
}

Things I've tried so far

  1. Generated new certificates and keys
  2. Added the certs to trusted store on the client
  3. I moved the server to a different directory which does not house the other node modules and that didnt help either. I was thinking other node modules might cause some kind of interference.

I'm running this on AWS and Ubuntu. I've spent couple of hours debugging this and any help will be appreciated!



via AGM

No comments:

Post a Comment