Heroku上的svg2png - TypeError:错误的参数

时间:2017-05-06 22:23:33

标签: node.js heroku promise

我在Heroku上遇到svg2png包的问题。它可以在我的本地机器上运行(Windows),但我无法在免费的dyno上工作,也不能在付费的(dobby和Standard x1)上工作。

这个想法是从维基百科获取svg并将其作为png返回。我暴露了两个端点:

  • / - 使用存储在单独文件中的代码
  • / simple - 相同的代码,但只是在回调中。

浏览两个端点时,我收到Heroku应用程序错误页面。这就是我在Heroku日志中获得的两个请求:

Bad argument TypeError: Bad argument
 at exports.spawn (child_process.js:378:9)
 at Object.execFile (/app/node_modules/pn/_promisify.js:35:27)
 at TypeError (native)
 at ChildProcess.spawn (internal/child_process.js:294:26)
 at Object.exports.execFile (child_process.js:143:15)
 at Promise.resolve.then (/app/node_modules/svg2png/lib/svg2png.js:13:33)
 at process._tickCallback (internal/process/next_tick.js:109:7)

你有什么想法吗?

index.js

const svg2png = require('svg2png')
const axios = require('axios')
const express = require('express')
const bodyParser = require('body-parser')
const conv = require('./conv')

const svgUrl = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg'

const app = express()

app.use(bodyParser.json({limit:'5mb'}))
app.use(bodyParser.urlencoded({extended:true,limit:'5mb'}))

app.get('/simple', function (req, res) {
    axios.get(svgUrl)
        .then(response => {
            return svg2png(response.data, { width: 150, height: 150 })
        })
        .then(pngFile => {
            var img = new Buffer(pngFile, 'base64')
            res.writeHead(200, {
                'Content-Type': 'image/png',
     'Content-Length': img.length
            })
            res.end(img)
        })
})

app.get('/', function (req, res) {
conv.send()    
        .then(pngFile => {
            var img = new Buffer(pngFile, 'base64')
            res.writeHead(200, {
                'Content-Type': 'image/png',
     'Content-Length': img.length
            })
            res.end(img)
        })
})

app.listen(process.env.PORT || 5000, function () {
    console.log('Application started')
})

conv.js

const axios = require('axios')
const svg2png = require('svg2png')

module.exports = {
    send() {
        const self = this;
                self.width = 300;
        self.height = 300;

        const svgUrl = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg'
        return new Promise((resolve, reject) => {
            axios.get(svgUrl)
            .then(response => 

            svg2png(response.data,
             { width: self.width, height: self.height })
            .then(img=>resolve(img))
            .catch(e => {
                console.log('Something went wrong during svg2png conversion.' + e)
                reject(e)
            }))
        });

    }
}

0 个答案:

没有答案