var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path')
var querystring = require("querystring");
const { createCanvas } = require('canvas');
var createCavas = function(data,fileName){
const canvas = createCanvas(240, 600);
const ctx = canvas.getContext('2d');
var rectH=20;
var rectW=60;
ctx.lineWidth = 1;
ctx.strokeStyle = "#ccc";
ctx.font = "12px Impact";
if(data.length>31){
data = data.slice(0,30)
}
for(var i= 0;i<data.length;i++){
ctx.fillText(i,30,rectH*i-5);
ctx.fillText(data[i].nickName,90,rectH*i-5);
ctx.fillText(data[i].userWinLoseMoney,150,rectH*i-5);
ctx.fillText(data[i].balance,210,rectH*i-5);
ctx.moveTo(rectW*i,0);
ctx.lineTo(rectW*i,canvas.height);
ctx.moveTo(0,rectH*i);
ctx.lineTo(canvas.width,rectH*i);
ctx.textAlign = "center";
}
ctx.stroke();
canvas.createPNGStream().pipe(fs.createWriteStream(path.join(__dirname, fileName + '.png')))
}
// 创建服务器
http.createServer( function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname;
var postData = "";
// 数据块接收中
request.on("data", function (postDataChunk) {
postData += postDataChunk;
});
request.on("end", function () {
console.log('数据接收完毕');
var params = JSON.parse(postData);//querystring.parse(postData)
var data = params.data;
var fileName = 'userscore__'+params.table+'__'+params.ShoeNo+'__'+params.GameNo+'__'+params.groupId;
createCavas(data,fileName)
});
// 从文件系统中读取请求的文件内容
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
}else{
response.write(data);
}
// 发送响应数据
response.end();
});
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

docker
容器内没安装字体导致