0

I am trying to create a parallel running server. I have two main concerns:

  1. Some aspects are blocking and will stop another request being processed
  2. Requests get mixed up and the wrong response goes to the wrong client.

Here is my code:

Server file:

var http = require('http');
var controller = require('controller.js');
http.createServer(function (request, response) {
    controller.handle(request, response);
}).listen(8080, '0.0.0.0');

Controller file

var resp = require('./resp.js');
var sqltest = require('/sql/sqltest.js');

exports.handle = function(request, response) {
    request.on('data', function(chunk) {
        var json = JSON.parse(chunk);
        var type = parseInt(json["requestType"]);

        switch(type) {
            case 0:
                sqltest.try(json, function(res){
                resp.send(response, res);
                });
            break;
       }
     });
}

SQL Test File

var mysql = require('mysql');
    exports.try = function(json, callback, res) {
    -- doo database stuff --
    callback(res);
    });
}

Response file

exports.send = function(response, res){
    response.writeHead(200, ......);
    response.end(res);
}

Basically, this server is going to very large and I can't have tens of thousands of lines of code all sitting in one file. By breaking it up, will I damage the performance in any way, will this cause any blocking code, will the server loose track of the correct client to respond to?

I'm still very new with node and javascript in general so perhaps I'm miss-understanding how the basic concepts work. Any advice or improvements would be a great help!

Nicholas Mordecai
  • 859
  • 2
  • 12
  • 33

1 Answers1

0

For concern #1, none of the code you have posted is blocking, but you're right to be concerned about this. Node is single threaded and writing code that is either blocking or takes a long time to compute is a bad idea in node. The good news is that node is very async oriented. While its possible to write blocking code in node, the way common libraries are designed, you have to go out of your way to write a blocking function.

This SO answer has a lot of good details to help wrap your head around it: https://stackoverflow.com/a/14797359/77501

For concern #2, I'm not sure what you mean. Are you afraid that a SQL async callback will get invoked that that a res var is somehow pointing to someone else's request?

Regarding the blurb at the bottom, having code all of your in one file would have no perceptible impact on performance. If anything your application might start up a tiny bit faster since it would have fewer modules to load.

Cramming tons of code into a single file is a bad idea for other reasons. Splitting your code up into modules will make your code more reusable, allowing you to write less of it. Less code is better. Giving your thousands of lines of code a clear architecture by splitting it up into modules that all "do one thing well" (aka cohesion) will also make your code easier to understand, easier to build off of, and easier debug.

Community
  • 1
  • 1
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50