繁体   English   中英

无法获得plotly + node.js来流经POST请求的数据

[英]Can't get plotly + node.js to stream data coming through POST requests

我正在尝试通过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();
};

当我POST使用卷曲的数据,例如curl http://localhost:3000/step --data "data=5" ,我可以看到,数据到达内部回调plotly.plot块,但从未plotly启动,并流数据。

在我之前使用的一些稍微复杂一些的服务器代码中,我也得到了错误,该错误可能相关也可能不相关,并且始终指向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);
    });

}

因此,我修改了代码,在Plotly.plot回调中包含console.log

在此处查看要点: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM