Javascript 使用节点js的gm包调整图像大小时出错

Javascript 使用节点js的gm包调整图像大小时出错,javascript,node.js,Javascript,Node.js,我已经尝试了一段时间了,但我一直得到错误: Error: Command failed: Invalid Parameter - /images 我安装了ImageMagick和gm软件包,所以这绝对不是问题所在 gm(imageLocation) .resize(100) // use your own width and height .write('here.jpg', function (err) { if (!err) console.

我已经尝试了一段时间了,但我一直得到错误:

Error: Command failed: Invalid Parameter - /images
我安装了ImageMagick和gm软件包,所以这绝对不是问题所在

    gm(imageLocation)
      .resize(100) // use your own width and height
      .write('here.jpg', function (err) {
        if (!err) console.log(' hooray! ');
        else console.log(err); 
      });
imageLocation
being
/images/3.jpg
。为什么这个错误一直发生?我看了文件

我在Windows 32位计算机上。我的服务器应该从文件夹中获取图像,调整其大小,然后显示它。看起来我必须写下调整大小的照片,然后再显示出来,但是写的过程总是出错,图像最终是空的

如果有办法跳过写作部分,直接显示照片,那也太棒了

谢谢


我使用的URL查询:
http://localhost:8123/images/3.jpg

完整代码:

var querystring = require('querystring'); //used for parsing parts of urls
    url = require('url'); 
    http = require('http'); 
    fs = require('fs'); 
    gm = require('gm').subClass({ imageMagick: true });; 

var server = http.createServer(); 

server.on('request', function(request, response){
    var parsed_url = url.parse(request.url, true); //true gets the query as well

    imageLocation = '.' + parsed_url.pathname;
    gm(imageLocation)
      .resize(100) // use your own width and height
      .write('here.jpg', function (err) {
        if (!err) console.log(' hooray! ');
        else console.log(err); 
      });


    if (getImage('here.jpg', response)){
        //image is displayed 
    }
    else{
    respond404(parsed_url.pathname, response); 
    }

})

function respond404(path, response){
    respond(404, "The requested path " + path + " was not found", response)  
}

function getImage(location, response)
{
    try{
        var img = fs.readFileSync(location);
        response.writeHead(200, {'Content-Type':'image/jpg'}); //parse this end
        response.end(img, 'binary');
        return true;
    }catch(e){
        return false; 
    }    
}

server.listen(8123); 

我可以通过如下更改您对gm的要求,使您的代码正常工作:

var gm = require('gm');
在本例中,我还必须记住使用正确的权限执行节点:

sudo node server.js

答案可以在Linux中使用(也可以在Mac中使用?)

对于Windows,我通过在
管理员模式下打开
命令行
并在那里启动我的服务器,使其正常工作