我正在尝试创建一个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中起作用?感谢任何帮助。谢谢