NodeJS: serve png generated by node-canvas

前端 未结 1 1192
遇见更好的自我
遇见更好的自我 2021-01-04 14:38

I would like to generate and serve a .png file using node-canvas. Using Express, this is what I have done so far:

draw_badge.js

func         


        
相关标签:
1条回答
  • 2021-01-04 14:47

    Try this in badge.js:

    var express = require('express');
    var router = express.Router();
    var draw = require('../lib/draw_badge.js');
    
    router.get('/show', function (req, res, next) {
      res.setHeader('Content-Type', 'image/png');
      draw().pngStream().pipe(res);
    });
    
    module.exports = router;
    

    Notice the code draw().pngStream().pipe(res);

    It will obtain a PNG stream from your Canvas and will pipe this stream to the response stream. Doing things this way, you don't need to call res.end(), because when your PNG stream will end, so will your response stream be ended.

    0 讨论(0)
提交回复
热议问题