Skip to main content
added 164 characters in body
Source Link
deloki
  • 1.8k
  • 2
  • 19
  • 27

EDIT3:

Is it ok that i am first creating the server and then connection to mongo, or should i create the server inside the MongoClient.connect(...) function?

EDIT3:

Is it ok that i am first creating the server and then connection to mongo, or should i create the server inside the MongoClient.connect(...) function?

added 240 characters in body
Source Link
deloki
  • 1.8k
  • 2
  • 19
  • 27

EDIT2: I removed the db.close statements.

Is cluster really the right solution here because i will be just upgrading the app to serve 4 concurrent requests which will still not be enough.

Should i completely refactor the server in some other way? I just want to be able to serve multiple requests for data from mongoDB.

 var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb; http.createServer(function (request, res) { console.log("creating server..."); MongoClient.connect("mongodb://127.0.0.1:27017/lalal", function(err, db) { if(err) { return console.dir(err); } if (request.method == 'POST') { var body = ''; response = res; request.on('data', function (data) { body += data; // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) { // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST request.connection.destroy(); } }); request.on('end', function () { var clientData = qs.parse(body); var parts = clientData.data.split("."); var collectionName = parts.shift(); var queryBig = parts.join("."); var queryParts = queryBig.split("("); var method = queryParts[0]; var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);'; break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); var indexOfQuote = s.indexOf('"') var restOfQuery = s.substring(indexOfQuote+1,s.length); var key = s.substring(0,indexOfQuote); query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery; } // Connect to the db // myDb = db; var collection = db.collection(collectionName); var command = 'collection.'+query+callback; console.log("command:"+command); eval(command); function findCallback(err, items){ console.log(items); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(items)); db.close(); } function insertCallback(err, objects) { console.log(objects); if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); } db.close(); } function removeCallback(err, numberOfRemovedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfRemovedDocs)); db.close(); } function saveCallback(err, result) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(result)); db.close(); } function updateCallback(err, numberOfUpdatedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfUpdatedDocs)); db.close(); } }); } }); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 
 var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb; http.createServer(function (request, res) { console.log("creating server..."); MongoClient.connect("mongodb://127.0.0.1:27017/lalal", function(err, db) { if(err) { return console.dir(err); } if (request.method == 'POST') { var body = ''; response = res; request.on('data', function (data) { body += data; // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) { // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST request.connection.destroy(); } }); request.on('end', function () { var clientData = qs.parse(body); var parts = clientData.data.split("."); var collectionName = parts.shift(); var queryBig = parts.join("."); var queryParts = queryBig.split("("); var method = queryParts[0]; var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);'; break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); var indexOfQuote = s.indexOf('"') var restOfQuery = s.substring(indexOfQuote+1,s.length); var key = s.substring(0,indexOfQuote); query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery; } // Connect to the db // myDb = db; var collection = db.collection(collectionName); var command = 'collection.'+query+callback; console.log("command:"+command); eval(command); function findCallback(err, items){ console.log(items); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(items)); db.close(); } function insertCallback(err, objects) { console.log(objects); if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); } db.close(); } function removeCallback(err, numberOfRemovedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfRemovedDocs)); db.close(); } function saveCallback(err, result) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(result)); db.close(); } function updateCallback(err, numberOfUpdatedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfUpdatedDocs)); db.close(); } }); } }); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 

EDIT2: I removed the db.close statements.

Is cluster really the right solution here because i will be just upgrading the app to serve 4 concurrent requests which will still not be enough.

Should i completely refactor the server in some other way? I just want to be able to serve multiple requests for data from mongoDB.

 var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb; http.createServer(function (request, res) { console.log("creating server..."); MongoClient.connect("mongodb://127.0.0.1:27017/lalal", function(err, db) { if(err) { return console.dir(err); } if (request.method == 'POST') { var body = ''; response = res; request.on('data', function (data) { body += data; // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) { // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST request.connection.destroy(); } }); request.on('end', function () { var clientData = qs.parse(body); var parts = clientData.data.split("."); var collectionName = parts.shift(); var queryBig = parts.join("."); var queryParts = queryBig.split("("); var method = queryParts[0]; var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);'; break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); var indexOfQuote = s.indexOf('"') var restOfQuery = s.substring(indexOfQuote+1,s.length); var key = s.substring(0,indexOfQuote); query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery; } // Connect to the db // myDb = db; var collection = db.collection(collectionName); var command = 'collection.'+query+callback; console.log("command:"+command); eval(command); function findCallback(err, items){ console.log(items); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(items)); } function insertCallback(err, objects) { console.log(objects); if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); } } function removeCallback(err, numberOfRemovedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfRemovedDocs)); } function saveCallback(err, result) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(result)); } function updateCallback(err, numberOfUpdatedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfUpdatedDocs)); } }); } }); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 
added 945 characters in body
Source Link
deloki
  • 1.8k
  • 2
  • 19
  • 27

But it seems that it cannot serve multiple requests in the same time for some reason.. (BTW i am using it locally)

EDIT: I sometimes get this error in my client app :

"Stream Error. URL: http://localhostlink:1337"

which i also get when the server is not running...

 var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb;    http.createServer(function (request, res) {  console.log("creating server..."); MongoClient.connect("mongodb://127.0.0.1:27017/lalal", function(err, db) { if(err) { return console.dir(err); } if (request.method == 'POST') {   var body = '';   response = res;   request.on('data', function (data) {   body += data;   // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB   if (body.length > 1e6) {   // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST   request.connection.destroy();   }   });   request.on('end', function () {   var clientData = qs.parse(body);   var parts = clientData.data.split(".");   var collectionName = parts.shift();   var queryBig = parts.join(".");   var queryParts = queryBig.split("(");   var method = queryParts[0];   var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);'; break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); console var indexOfQuote = s.logindexOf("query:"+query'"'); console var restOfQuery = s.logsubstring("method:"+methodindexOfQuote+1,s.length); console var key = s.logsubstring("collection:"+collectionName0,indexOfQuote); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);';  break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != indexHelper-1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); var indexOfQuote = s.indexOf('"')  var restOfQuery+ ='new s.substringObjectId(indexOfQuote+1,s.length); var key ="'+key s.substring(0,indexOfQuote+'"); '+restOfQuery; query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery;      } // Connect to the db  MongoClient.connect("mongodb://127.0.0.1:27017/lalala", function(err, db) { if(err) { return console.dir(err); }   myDb = db; var collection = db.collection(collectionName);  // myDb = db;  var collection = db.collection(collectionName); var command = 'collection.'+query+callback;   console.log("command:"+command);   eval(command); });  } function findCallback(err, items); }{ }).listen(1337, '127.0.0 console.1'log(items); console.log('Server running at http response.writeHead(200, {'Content-Type':/ 'text/127.0plain'}); response.0end(JSON.1:1337/'stringify(items)); function findCallback(err, items db.close(){; console.log(items); } response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify function insertCallback(items)err, objects); { myDb console.closelog(objects);  if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); } function insertCallback(err, objects db.close();  { } console.log function removeCallback(objectserr, numberOfRemovedDocs); { if  response.writeHead(err200, {'Content-Type': 'text/plain'});  console response.warnend(errJSON.messagestringify(numberOfRemovedDocs)); if (err && err.message db.indexOfclose('E11000 ');  !== -1 } function saveCallback(err, result) {     response.writeHead(200, {'Content-Type': 'text/plain'});   response.end('"error":"_idJSON.stringify(result));  already exists"' db.close();   }   else function updateCallback(err, numberOfUpdatedDocs) {     response.writeHead(200, {'Content-Type': 'text/plain'});   response.end(JSON.stringify(objectsnumberOfUpdatedDocs)); db.close(); } }); }  });  myDb}).closelisten(1337, '127.0.0.1'); } function removeCallback(err, numberOfRemovedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfRemovedDocs)); myDb.close(); }  function saveCallback(err, result) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(result)); myDb.close(); }  function updateCallbackconsole.log(err, numberOfUpdatedDocs) { 'Server running response.writeHead(200,at {'Content-Type'http: 'text/plain'}); response/127.end(JSON0.stringify(numberOfUpdatedDocs)); myDb0.close(1:1337/'); } 

But it seems that it cannot serve multiple requests in the same time for some reason..

var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb; http.createServer(function (request, res) { if (request.method == 'POST') { var body = ''; response = res; request.on('data', function (data) { body += data; // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) { FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST request.connection.destroy(); } }); request.on('end', function () { var clientData = qs.parse(body); var parts = clientData.data.split("."); var collectionName = parts.shift(); var queryBig = parts.join("."); var queryParts = queryBig.split("("); var method = queryParts[0]; var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);';  break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length); var indexOfQuote = s.indexOf('"')  var restOfQuery = s.substring(indexOfQuote+1,s.length); var key = s.substring(0,indexOfQuote);  query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery;      } // Connect to the db  MongoClient.connect("mongodb://127.0.0.1:27017/lalala", function(err, db) { if(err) { return console.dir(err); }   myDb = db; var collection = db.collection(collectionName);  var command = 'collection.'+query+callback; console.log("command:"+command); eval(command); });  }); } }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); function findCallback(err, items){ console.log(items); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(items)); myDb.close(); } function insertCallback(err, objects) { console.log(objects); if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); }    myDb.close(); } function removeCallback(err, numberOfRemovedDocs) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfRemovedDocs)); myDb.close(); }  function saveCallback(err, result) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(result)); myDb.close(); }  function updateCallback(err, numberOfUpdatedDocs) {  response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(numberOfUpdatedDocs)); myDb.close(); } 

But it seems that it cannot serve multiple requests in the same time for some reason.. (BTW i am using it locally)

EDIT: I sometimes get this error in my client app :

"Stream Error. URL: http://localhostlink:1337"

which i also get when the server is not running...

 var http = require('http'); var qs = require('querystring'); var mongo =require('mongodb'); var MongoClient = mongo.MongoClient; var result; var response; var ObjectId = require('mongodb').ObjectID; var myDb;    http.createServer(function (request, res) {  console.log("creating server..."); MongoClient.connect("mongodb://127.0.0.1:27017/lalal", function(err, db) { if(err) { return console.dir(err); } if (request.method == 'POST') {   var body = '';   response = res;   request.on('data', function (data) {   body += data;   // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB   if (body.length > 1e6) {   // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST   request.connection.destroy();   }   });   request.on('end', function () {   var clientData = qs.parse(body);   var parts = clientData.data.split(".");   var collectionName = parts.shift();   var queryBig = parts.join(".");   var queryParts = queryBig.split("(");   var method = queryParts[0];   var query = queryParts.join("("); console.log("query:"+query); console.log("method:"+method); console.log("collection:"+collectionName); var callback; switch(method){ case 'find': callback = '.toArray(findCallback);'; break; case 'insert': query = query.substring(0, query.length - 1); callback = ',insertCallback);'; break; case 'remove': query = query.substring(0, query.length - 1); callback = ',removeCallback);' break; case 'save': query = query.substring(0, query.length - 1); callback = ',saveCallback);' break; case 'update': query = query.substring(0, query.length - 1); callback = ',updateCallback);' break; } if(query.indexOf('"_id"') != -1) { var indexHelper = query.indexOf('"_id"')+7; var s = query.substring(indexHelper, query.length);  var indexOfQuote = s.indexOf('"')  var restOfQuery = s.substring(indexOfQuote+1,s.length);  var key = s.substring(0,indexOfQuote); query = query.substring(0,indexHelper-1) + 'new ObjectId("'+key +'")'+restOfQuery; } // Connect to the db // myDb = db;  var collection = db.collection(collectionName); var command = 'collection.'+query+callback;   console.log("command:"+command);   eval(command);  function findCallback(err, items){  console.log(items);  response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(items));  db.close();  }  function insertCallback(err, objects) {  console.log(objects);  if (err) console.warn(err.message); if (err && err.message.indexOf('E11000 ') !== -1) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('"error":"_id already exists"'); } else{ response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(JSON.stringify(objects)); }  db.close();   }  function removeCallback(err, numberOfRemovedDocs) {   response.writeHead(200, {'Content-Type': 'text/plain'});   response.end(JSON.stringify(numberOfRemovedDocs));  db.close();   } function saveCallback(err, result) {     response.writeHead(200, {'Content-Type': 'text/plain'});   response.end(JSON.stringify(result));   db.close();   }    function updateCallback(err, numberOfUpdatedDocs) {     response.writeHead(200, {'Content-Type': 'text/plain'});   response.end(JSON.stringify(numberOfUpdatedDocs)); db.close(); } }); } }); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 
Source Link
deloki
  • 1.8k
  • 2
  • 19
  • 27
Loading