我正在尝试创建一个 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/