无法获得plotly + node.js来传输通过POST请求传输的数据

时间:2015-12-18 01:03:57

标签: javascript node.js plotly

我正试图通过POST请求将我的服务器收到的数据流式传输到http://localhost:3000/step

在plotly-nodejs / examples中的rest-example.js上构建,这是我的服务器代码(我已经模糊了我的用户名,apikey和令牌):

'use strict';

var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');
var events = require('events');
var eventEmitter = new events.EventEmitter();

var app = express();
var server = require('http').Server(app);
var port = process.env.PORT || 3000;
server.listen(port);

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.post('/step', function(req, res) {
    var data = req.body.data;
    eventEmitter.emit('step', data);
    res.end('ok');
});

var plotly = require('plotly')('username', 'apikey');
var token = 'token';

var dataInit = [{x:[], y:[], stream: { token: token, maxpoints: 10 } }];
var layout = {fileopt : "extend", filename : "REST-test"};

plotly.plot(dataInit, layout, function (err, msg) {
    if(err) return console.error('step data error', err.stack);

    var stream = plotly.stream(token, function() {});

    eventEmitter.on('step', function(data) {
        console.log('sending to plotly: ' + data + ' steps');

        var streamObject = JSON.stringify({ x: getDateString(), y: data });
        stream.write(streamObject+'\n');
    });
});

function getDateString() {
    var d = new Date();
    return d.toLocaleString();
};

当我使用cURL POST数据时,例如curl http://localhost:3000/step --data "data=5",我可以看到数据到达plotly.plot块内的回调,但是从来没有启动并流式传输数据。

在我之前正在处理的一些稍微复杂的服​​务器代码中,我也得到了可能相关或不相关的错误,并且始终指向plotly.plot块的开头。

cb(null, body);
        ^
SyntaxError: Unexpected end of input

这是完整的错误堆栈:

/home/plotly-testing/node_modules/plotly/index.js:305
        cb(null, body);
        ^
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at /home/plotly-testing/node_modules/plotly/index.js:72:25
    at IncomingMessage.<anonymous> (/home/plotly-testing/node_modules/plotly/index.js:305:9)
    at IncomingMessage.emit (events.js:129:20)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)
---------------------------------------------
    at IncomingMessage.Readable.on (_stream_readable.js:671:33)
    at parseRes (/home/plotly-testing/node_modules/plotly/index.js:304:9)
    at ClientRequest.<anonymous> (/home/plotly-testing/node_modules/plotly/index.js:71:9)
    at ClientRequest.emit (events.js:107:17)
    at HTTPParser.parserOnIncomingClient (_http_client.js:426:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
    at TLSSocket.socketOnData (_http_client.js:317:20)
---------------------------------------------
    at new ClientRequest (_http_client.js:93:10)
    at Object.exports.request (http.js:49:10)
    at Object.exports.request (https.js:136:15)
    at Plotly.plot (/home/plotly-testing/node_modules/plotly/index.js:70:21)
    at Object.<anonymous> (/home/plotly-testing/index.js:175:8)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

plotly/index.js的第305行指向以下方法,这似乎表明我的一个回调中出现了问题,但我不确定。

// response parse helper fn
function parseRes (res, cb) {
    var body = '';
    if ('setEncoding' in res) res.setEncoding('utf-8');

    res.on('data', function (data) {
        body += data;
        if (body.length > 1e10) {
            // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQ
            res.connection.destroy();
            res.writeHead(413, {'Content-Type': 'text/plain'});
            res.end('req body too large');
            return cb(new Error('body overflow'));
        }
    });

    res.on('end', function () {
        cb(null, body);
    });

}

1 个答案:

答案 0 :(得分:2)

所以我修改了代码,在console.log回调中添加了Plotly.plot

见这里的要点: https://gist.github.com/alexander-daniel/b36f9be78abbbaa4847e#file-index-js-L33

通过这种方式,我们可以看到Plotly返回了我们可以查看的图形URL。 https://gist.github.com/alexander-daniel/b36f9be78abbbaa4847e#file-console_output-L5

这应解决第一个问题。

就第二个问题而言,似乎问题有两个: - 库中的JSON.parse调用没有包含在try / catch中,所以看起来如果流服务器返回任何不是JSON的东西,这将会中断。

我们正在研究流媒体服务器错误返回,但我已经在这里打开了这个问题:API库中的try / catch块。

github.com/plotly/plotly-nodejs/issues/37