Saturday, 20 May 2017

Node JS server randomly crashing - unknown error

I'm running a Node JS server (v7.10.0 on Centos 7 64bit VPS) through Nodemon and PM2. Lately I've been get this error:

[STREAMING] Now streaming realtime logs for [0] process
0|nodemon  | events.js:163
0|nodemon  |       throw er; // Unhandled 'error' event
0|nodemon  |       ^
0|nodemon  | Error: read ECONNRESET
0|nodemon  |     at exports._errnoException (util.js:1050:11)
0|nodemon  |     at TCP.onread (net.js:582:26)
0|nodemon  | [nodemon] app crashed - waiting for file changes before starting...

It isn't referencing any of the my files and it happens randomly and not when I upload a change so I think it may not be my code. However because it mentions "TCP" it may be what I'm doing to post requests to '/socket'? These are the dependencies I'm relying on:

"dependencies": {
    "body-parser": "*",
    "ejs": "*",
    "express": "*",
    "express-session": "*",
    "express-subdomain": "*",
    "https": "*",
    "mongodb": "*",
    "uuidv4": "*"
  }

This is my app.js (main file) code:

const fs = require('fs');
const http = require('http');
const https = require('https');
const uuid = require('uuid/v4');
const path = require('path');
const bodyParser = require('body-parser');
const express = require('express');
const expressSession = require('express-session');
const app = module.exports = express();

app.set('view engine', 'ejs');
app.set('trust proxy', true);
app.use(bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressSession({
    secret: uuid(),
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true }
}));

/* Is there a better way to do this? Still learning Node JS */
['//',
'plans',
'contact-us',
'affiliate-program',
'terms-of-service',
'account/login',
'account/signup'
].forEach(function(value) {
    app.get('/' + value.replace('//', ''), function(req, res) {
        res.render(value.replace('//', 'index'), {res: res, session: req.session});
    });
});

/* Possible location of error? But why? */
app.post('/socket', function(req, res) {
    /* Social count variable will be used soon */
    const socialCount = req.body.socialCount;
    const socket = require('net').Socket();
    socket.connect('<removed>');
    socket.write(JSON.stringify(req.body));
    socket.end();
});

require('./mongo');
require('./rest');

const ssl = {
    key: fs.readFileSync('<removed>.key'),
    cert: fs.readFileSync('<removed>.crt'),
    ca: fs.readFileSync('<removed>.crt'),
    requestCert: true,
    rejectUnauthorized: false
};

/* Listening on 8080 because we're moving from PHP to Node JS and our designer is still working on the PHP branch */
https.createServer(ssl, app).listen(8080, function () {
    console.log('Starting server on port 8080');
});

/*

Redirect http to https

http.createServer(function(req, res) {
    res.writeHead(301, { 'Location': 'https://' + req.headers.host + req.url });
    res.end();
}).listen(80);

*/

I'm sure there are a couple things that are sub-par, if anyone has any advice let me know please. I'm still learning how to use Node JS.

The mongo and rest files I require aren't actually being ran currently, those shouldn't be the problem.

The crash seems to randomly happen, not when I upload a file or even when anyone is visiting the site. However as mentioned above, it may be linked with how I'm doing sockets due to it saying "TCP" in the error? Any ideas? Thanks for reading



via GatorFlores

No comments:

Post a Comment