2016-10-05 125 views
0

我非常喜欢这里没有堆栈问题。 如何创建一个nodejs http模块文件上传?我有一个服务器,巫婆会reaceave一个文件+一些表单参数,我如何得到图像(我将需要调整图像大小,然后上传到其他服务器)。nodejs http模块 - 图片上传

我当前的代码:

17 // Searches Elasticsearch and return's firebase references 
18 http.createServer(function(request, response) { 
19 var method = request.method; 
20 var url = request.url; 
21 var body = []; 
22 
23 // Fetches body and pushes into array 
24 request.on('error', function(err) { 
25  console.error(err); 
26 }).on('data', function(chunk) { 
27  body.push(chunk); 
28 }).on('end', function() { 
29  body = Buffer.concat(body).toString(); 
30  response.statusCode = 200; 
31  response.setHeader('Content-Type', 'application/json'); 
32 
33  // Mini router 
34  if(url == "/save_photo") { 
35  upload_photo.resizeAndSavePhoto(request); 
36  response.write("body"); 
37  response.end(); 
38  } else { 
39  search.searchElasticsearchForMaches(request, url, elasticsearchUrl, elasticsearchPort).then((data) => { 
40   response.write(data); 
41   response.end(); 
42  }); 
43  } 
44 }); 
45 }).listen(8080); 



    1 var http = require('http'); 
    2 
    3 module.exports = { 
    4 resizeAndSavePhoto: function(body) { 
    5  console.dir(body.files); 
    6 } 
    7 }; 

回答

1

您可以使用ImageMagickGraphicsMagick我宁愿gm,因为它更多的是表现为node.Graphic magick更好的稳定应该在服务器上安装可

你可以这样安装gm

npm install gm 

欲了解更多详细信息请点击这里 http://aheckmann.github.io/gm/docs.html

+0

但我怎么可以解析的形象呢?像从请求? – IvRRimUm

+0

这适用于我的调整大小,所以我会upvote。 – IvRRimUm