Saturday 22 April 2017

Node.js server hangs occasionally

I have this node.js server. The problem is sometimes I notice that it gets stuck. That is the client can make requests, but the server doesn't respond, rather it doesn't end the response, it just gets stuck in the server side. If I look in client side dev tools on the http request, it has a gray circle icon meaning waiting for server response. Even if I wait 10 minutes, nothing happens.

The server side also writes things to console on the requests, which doesn't happen when it gets stuck. On the node.js console, if I then press ctrl+c when it got stuck, I then suddenly see all the console.log messages just appear on the console, and at the same time, the dev tools, recieves all the responses from the server side, i.e. the gray circle changes to green.

Does anyone know what this problem is?

Thanks

var express = require('express');
var https = require("https");
var fse = require("fs-extra");
var bodyParser = require('body-parser');

// INFO

var root = __dirname + '/public';

setUpServer();

// SET UP SERVER

function setUpServer() {
    var app = express();

    app.use(express.static(root));
    app.use(bodyParser.urlencoded({ extended: false }))

    app.get('/', function (req, res) {
        var dest = 'index.html';
        res.sendFile(dest, { root: root + "/pong" });
    });
    app.post('/get_brain', function (req, res) {
        res.end("1");
        console.log('Sent master brain to a client!');
    });
    app.post('/train_and_get_brain', function (req, res) {
        res.end("1");
        console.log('Sent master brain to a client!');
    });



    var privateKey  = fse.readFileSync('sslcert/key.pem', 'utf8');
    var certificate = fse.readFileSync('sslcert/cert.pem', 'utf8');
    var credentials = {key: privateKey, cert: certificate};

    var httpsServer = https.createServer(credentials, app);
    httpsServer.listen(process.env.PORT || 3000, function () {
        var host = httpsServer.address().address;
        var port = httpsServer.address().port;

        console.log('AI started at https://%s:%s', host, port);
    });
}



via omega

No comments:

Post a Comment