I am trying to create a parallel running server. I have two main concerns:
- Some aspects are blocking and will stop another request being processed
- 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!