Tuesday, 15 January 2013

javascript - Execute function from String in node.js -



javascript - Execute function from String in node.js -

is there way execute function string passed request parameter , homecoming result in node.js ? imagine request contains string:

"db.users.find()"

as parameter

if knew wanted execute statement this:

db.collection('users', function(err, collection) { collection.find().toarray(function(err, items) { res.send(items); }); });

what want parser execute given statement , returned results.

i tried this:

exports.execstatement = function(req, res) { //var statemnet = req.params.statement; var statement = "collection.find().toarray(function(err, items) { res.send(items);});"; db.collection('users', function(err, collection) { eval(statement) }); };

this code gives me error:

var statement = "collection.find().toarray(function(err, prdel) { ... syntaxerror: unexpected token illegal

what wrong ? why can't execute code stored in string ? after works figure out how execute code passed parameter in request.

is approach possible ?

the sec illustration shows how can add together function db.

http://mongodb.github.com/node-mongodb-native/api-generated/db.html#eval

however don't. there 3 main reasons

in 2.2 , before spidermonkey single threaded meaning abysmal performance "stored procedures" an eval requires write lock duration of operation (unless set nolock , improve not write or fail). stops write operations on db until function finishes executing. injection attacks that's been covered above.

javascript node.js mongodb parameters

No comments:

Post a Comment