从Meteor服务器端路由使用node-rio [英] Using node-rio from Meteor Server Side Route

查看:82
本文介绍了从Meteor服务器端路由使用node-rio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Meteor通过服务器端路由访问Rserve服务器以调用R代码.允许访问Rserve的Node.js模块是 node-rio 我已经使用过Async.wrap meteorhacks:npm 流星包中的函数来包装评估"方法.当我尝试在浏览器中访问路由路径"/rio"时,将"1"写入控制台日志,这对于R中命令"1"的评估是正确的,但是Chrome挂起并显示消息"Waiting"本地主机".它不会进行到下一行,并且永远不会显示结果:1".消息"net :: ERR_EMPTY_RESPONSE最终显示在Chrome中.

I'm trying to access an Rserve server using Meteor through a server side route to call R code. The Node.js module that allows access to Rserve is node-rio I have used the Async.wrap function in the meteorhacks:npm Meteor package to wrap the "evaluate" method. When I try to access the route path "/rio" in a browser, I get "1" written to the console log, which is correct for the evaluation of the command "1" in R, but Chrome hangs with the message "Waiting for localhost". It doesn't proceed to the next line and "Result: 1" is never shown. The message "net::ERR_EMPTY_RESPONSE eventually shows up in Chrome.

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

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

"evaluate"具有回调函数参数,但包装在options参数中并可以访问:

"evaluate" has a callback function parameter, but it is wrapped in an options parameter and accessed:

rio.evaluate(R_COMMAND, {callback: CALLBACK_FUNCTION})

推荐答案

此答案将为'packageVersion("base")'产生预期的结果,它使用Node模块

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:

如何从Meteor自己的回调中调用异步方法?/a>

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));
    }
});

这篇关于从Meteor服务器端路由使用node-rio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆