node.js - 使用expressjs时highcharts-export-server出错

标签 node.js rest express highcharts export

我正在尝试创建一个 REST API,用于使用 Node 和 Express 导出 Highcharts。下面是我的代码,

var express = require('express');
var path = require('path');
var bodyParser = require("body-parser");
const chartExporter = require("highcharts-export-server");

var app = express();
app.use(bodyParser.json());

var port = 1200;

app.post('/chart', function(req, res){
    console.log("Json Body from the post request\n", req.body);
    chartExporter.initPool();
    chartExporter.export(res.body, function(error, response){
        console.log("Error from the function", error);
        var imageb64 = response.data;
        console.log("Image From the exporter\n", imageb64);
    });
    chartExporter.killPool();
    console.log("\nPool Killed");
    return "Server Returns successfully!";
});

module.exports = app;

图表选项如下,作为 API 正文传递,

{
   "type": "png",
   "options": {
       "chart": {
           "type": "pie"
       },
       "title": {
           "text": "Heading of Chart"
       },
       "plotOptions": {
           "pie": {
               "dataLabels": {
                   "enabled": true,
                   "format": "<b>{point.name}</b>: {point.y}"
               }
           }
       },
       "series": [
           {
               "data": [
                   {
                       "name": "a",
                       "y": 100
                   },
                   {
                       "name": "b",
                       "y": 20
                   },
                   {
                       "name": "c",
                       "y": 50
                   }
               ]
           }
       ]
   }
}

如果我运行 API,我会收到以下错误,

TypeError: Cannot read property 'svg' of undefined
    at Object.module.exports [as export] (D:\Dev\hc-export-server\node_modules\highcharts-export-server\lib\chart.js:270:23)
    at D:\Dev\hc-export-server\server.js:60:22
    at Layer.handle [as handle_request] (D:\Dev\hc-export-server\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Dev\hc-export-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Dev\hc-export-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Dev\hc-export-server\node_modules\express\lib\router\layer.js:95:5)
    at D:\Dev\hc-export-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\Dev\hc-export-server\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Dev\hc-export-server\node_modules\express\lib\router\index.js:275:10)
    at D:\Dev\hc-export-server\node_modules\body-parser\lib\read.js:130:5

但是,如果我在 API 之外单独运行同一组代码(假设在同一个文件中),我将从导出器函数返回响应。就像下面这样。响应正在终端中打印。

var express = require('express');
var path = require('path');
var bodyParser = require("body-parser");
const chartExporter = require("highcharts-export-server");

var app = express();
app.use(bodyParser.json());

var port = 1200;

const chartDetails = {
   "type": "png",
   "options": {
       "chart": {
           "type": "pie"
       },
       "title": {
           "text": "Heading of Chart"
       },
       "plotOptions": {
           "pie": {
               "dataLabels": {
                   "enabled": true,
                   "format": "<b>{point.name}</b>: {point.y}"
               }
           }
       },
       "series": [
           {
               "data": [
                   {
                       "name": "a",
                       "y": 100
                   },
                   {
                       "name": "b",
                       "y": 20
                   },
                   {
                       "name": "c",
                       "y": 50
                   }
               ]
           }
       ]
   }
};
chartExporter.initPool();
chartExporter.export(chartDetails, function(error, response){
        var imageb64 = response.data;
        console.log("Image From the exporter\n", imageb64);
    });
app.listen(port, function(){
    console.log("Server started at port ",  port);
});

app.post('/chart', function(req, res){
    console.log("Json Body from the post request\n", req.body);
    chartExporter.initPool();
    chartExporter.export(res.body, function(error, response){
        console.log("Error from the function", error);
        var imageb64 = response.data;
        console.log("Image From the exporter\n", imageb64);
    });
    chartExporter.killPool();
    console.log("\nPool Killed");
    return "Server Returns successfully!";
});

module.exports = app;

如何使其在 REST API 中工作?感谢任何帮助。谢谢

最佳答案

你犯了两个错误:

  • 您应该使用req.body而不是res.body
  • chartExporter.killPool() 应该位于 chartExporter.export 方法回调内
<小时/>

正确代码:

var express = require('express');
var path = require('path');
var bodyParser = require("body-parser");
const chartExporter = require("highcharts-export-server");

var app = express();
app.use(bodyParser.json());

var port = 1200;

app.post('/chart', function(req, res){
    console.log("Json Body from the post request\n", req.body);
    chartExporter.initPool();
    chartExporter.export(req.body, function(error, response){
        console.log("Error from the function", error);
        var imageb64 = response.data;
        console.log("Image From the exporter\n", imageb64);
        chartExporter.killPool();
        process.exit(1);

        res.send(imageb64);
    });
});

module.exports = app;

关于node.js - 使用expressjs时highcharts-export-server出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095866/

相关文章:

javascript - Cropper.js - 第一个参数是必需的,并且必须是 <img> 或 <canvas> 元素

node.js - 响应拦截器的 CatchError 函数执行多次 - Angular 6

javascript - 如何在 POST 请求期间将键值对添加到 req.body 字典中

javascript - 找不到 Node.js/socket.io/socket.io.js express 4.0

javascript - ExpressJS Route 中的正则表达式

node.js - 如何在 ReactJS 应用程序中为 markdown/LaTex 代码添加预览功能

css - node.js - 当参数 express.js 太长时无法加载 css

iOS 最成熟的 REST 服务库

rest - 计算 REST API 的 ETag

java - 对象化后端和客户端之间的 Key/Ref 往返 [无 GWT]