Using node-rio from Meteor Server Side Route

…衆ロ難τιáo~ 提交于 2019-12-06 15:38:28

Two errors:

  1. as you already noted rio expects the callback as a value to the callback field in the parameters, not straight in the arguments themselves.
  2. your result ("1") wasn't actually valid JSON.

This works:

Router.route('/rio', function() {                                               
    var rio = Meteor.npmRequire('rio');                                         
    var evalSync = Async.wrap(function(exp, callback) {                         
        rio.evaluate(exp, {callback: callback});                                
    });                                                                         
    var result = evalSync('1');                                                 
    console.log("Result: " + result);                                           

    // JSON                                                                     
    this.response.writeHead(200, {'Content-Type': 'application/json'});         
    this.response.end(JSON.stringify({result: result}));                        
}, {                                                                            
    where: 'server'                                                             
});
cannin

This answer will produce the expected result for 'packageVersion("base")', it uses the Node module rserve-client instead of rio to connect to Rserve. The route also handles the parameter 'pkg'. This is the StackOverflow question that pointed me in the right direction:

How to call async method from Meteor own callbacks?

Router.route('rserve', {
    path: '/rserve/:pkg',
    where: 'server',
    action:  function() {
        var r = Meteor.npmRequire("rserve-client");
        var Future = Meteor.npmRequire("fibers/future");
        var fut = new Future();

        var cmd = 'packageVersion("' + this.params.pkg + '")';

        var callR = function (input) {
            r.connect('127.0.0.1', 6311, function (err, client) {
                client.evaluate(input, function (err, ans) {
                    console.log("Result: " + ans);
                    client.end();

                    fut.return(ans);
                });
            });

            return fut.wait();
        };

        var result = callR(cmd);

        this.response.writeHead(200, {'Content-Type': 'application/json'});
        this.response.end(JSON.stringify(result));
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!