Rendering a Base64 PNG with Express

后端 未结 3 1347
小鲜肉
小鲜肉 2020-12-08 11:02

My Node.js server has something that looks like the following:

app.get(\"/api/id/:w\", function(req, res) {
    var data = getIcon(req.params.w);
});
         


        
相关标签:
3条回答
  • 2020-12-08 11:12

    I had to do a bit of manipulation first to get mine in the right format, but this worked great:

      var base64Data = data.replace(/^data:image\/png;base64,/, '');
    
    0 讨论(0)
  • 2020-12-08 11:12

    Using "base64-img" component:

    app.get('/image1', function(req, res) {
    
      var image1 = 'image1.jpg';
    
      var base64Img = require('base64-img');
      var imageData1 = base64Img.base64Sync(image1);
      var base64Data = imageData1.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
      var img = Buffer.from(base64Data, 'base64');
    
      res.writeHead(200, {
        'Content-Type': 'image/png',
        'Content-Length': img.length
      });
      res.end(img);
    
    });
    
    0 讨论(0)
  • 2020-12-08 11:21

    Yes you can encode your base64 string and return it to the client as an image:

    server.get("/api/id/:w", function(req, res) {
        var data = getIcon(req.params.w);
        var img = Buffer.from(data, 'base64');
    
       res.writeHead(200, {
         'Content-Type': 'image/png',
         'Content-Length': img.length
       });
       res.end(img); 
    });
    
    0 讨论(0)
提交回复
热议问题