我想询问 Highchart 导出服务器,直到节点 14 我们的导出服务器一直在工作,当我将节点 14 更新到 20 最新版本时,我的导出 highchart 导出停止工作。 我尝试了不同的方法,但没有一个有效,任何人都可以帮助我。 我使用节点 20 的所有版本的高图表导出服务器,但没有结果图表未绘制。
Highchart 导出服务器 3.0.5
我通过添加package.js找到了解决方案
"exports": {
".": {
"import": "./dist/index.esm.js",
"require": "./dist/index.cjs"
}
},
"repository": {
"url": "https://github.com/highcharts/node-export-server",
"type": "git"
},
"bin": {
"highcharts-export-server": "./bin/cli.js"
},
"scripts": {
"install": "node install.js",
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
在node.js文件中
let exporter = require('highcharts-export-server');
exporter.killPool();
如果不起作用,请添加 .env 文件并添加以下代码。
# Export config
EXPORT_DEFAULT_TYPE = png
EXPORT_DEFAULT_CONSTR = chart
EXPORT_DEFAULT_HEIGHT = 400
EXPORT_DEFAULT_WIDTH = 600
EXPORT_DEFAULT_SCALE = 1
EXPORT_RASTERIZATION_TIMEOUT = 1500
# Highcharts config
HIGHCHARTS_VERSION = latest
HIGHCHARTS_CDN = https://code.highcharts.com/
HIGHCHARTS_FORCE_FETCH = false
HIGHCHARTS_CORE_SCRIPTS =
HIGHCHARTS_MODULES =
HIGHCHARTS_INDICATORS =
# Custom code config
HIGHCHARTS_ALLOW_CODE_EXECUTION = false
HIGHCHARTS_ALLOW_FILE_RESOURCES = false
# Server config
HIGHCHARTS_SERVER_ENABLE = false
HIGHCHARTS_SERVER_HOST = 0.0.0.0
HIGHCHARTS_SERVER_PORT = 7801
# Server SSL config
HIGHCHARTS_SERVER_SSL_ENABLE = false
HIGHCHARTS_SERVER_SSL_FORCE = false
HIGHCHARTS_SERVER_SSL_PORT = 443
HIGHCHARTS_SERVER_SSL_CERT_PATH =
# Server rate limiting config
HIGHCHARTS_RATE_LIMIT_ENABLE = false
HIGHCHARTS_RATE_LIMIT_MAX = 10
HIGHCHARTS_RATE_LIMIT_WINDOW = false
HIGHCHARTS_RATE_LIMIT_DELAY = 0
HIGHCHARTS_RATE_LIMIT_TRUST_PROXY = false
HIGHCHARTS_RATE_LIMIT_SKIP_KEY =
HIGHCHARTS_RATE_LIMIT_SKIP_TOKEN =
# Pool config
HIGHCHARTS_POOL_MIN_WORKERS = 8
HIGHCHARTS_POOL_MAX_WORKERS = 8
HIGHCHARTS_POOL_WORK_LIMIT = 40
HIGHCHARTS_POOL_ACQUIRE_TIMEOUT = 5000
HIGHCHARTS_POOL_CREATE_TIMEOUT = 5000
HIGHCHARTS_POOL_DESTROY_TIMEOUT = 5000
HIGHCHARTS_POOL_IDLE_TIMEOUT = 30000
HIGHCHARTS_POOL_CREATE_RETRY_INTERVAL = 200
HIGHCHARTS_POOL_REAPER_INTERVAL = 1000
HIGHCHARTS_POOL_BENCHMARKING = false
HIGHCHARTS_POOL_LISTEN_TO_PROCESS_EXITS = true
# Logging config
HIGHCHARTS_LOG_LEVEL = 4
HIGHCHARTS_LOG_FILE = highcharts-export-server.log
HIGHCHARTS_LOG_DEST = log/
# UI config
HIGHCHARTS_UI_ENABLE = true
HIGHCHARTS_UI_ROUTE = /
# Other config
HIGHCHARTS_NO_LOGO = false
# Proxy config
PROXY_SERVER_HOST =
PROXY_SERVER_PORT =
PROXY_SERVER_TIMEOUT =
然后是索引文件
const Hapi = require('@hapi/hapi');
const exporter = require('highcharts-export-server');
const fs = require('fs');
exporter.killPool();
const init = async () => {
const server = Hapi.server({
port: 3000,
host: 'localhost',
});
server.route({
method: 'GET',
path: '/export-chart',
handler: async (request, h) => {
const exportSettings = {
export: {
type: 'pdf',
options: {
title: {
text: 'My Chart',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr'],
},
series: [
{
type: 'bar',
data: [1, 3, 2, 4],
},
{
type: 'line',
data: [5, 3, 4, 2],
},
],
},
},
};
// Set the new options and merge it with the default options
const options = exporter.setOptions(exportSettings);
try {
await exporter.initPool(options);
exporter.startExport(exportSettings, async function (res, err) {
if (err) {
console.log(err);
return h.response('Error exporting chart').code(500);
}
// Save the PDF file locally
const outputFile = 'downloads/exported-chart3.pdf';
fs.writeFileSync(outputFile, Buffer.from(res.data, 'base64'));
console.log(`PDF file saved as: ${outputFile}`);
return h.response(`PDF file saved as: ${outputFile}`);
});
} catch (error) {
console.error(error);
return h.response('Internal Server Error').code(500);
}
return "File save successfully.;"
},
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
这是我的工作示例。