0

I am pretty supcied there is not a stack question about this. How can i create a nodejs http module file upload?

I have a server, witch will reaceave a file + some form parameters, how can i get the image( i will need to resize the image, then upload to other server later ).

My current code:

 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 };

2 Answers 2

1

You can use either ImageMagick or GraphicsMagick i would prefer gm as it is more better in performance and stable for node.Graphic magick should be install on your server

you can install gm like this

npm install gm

For more details check this http://aheckmann.github.io/gm/docs.html

Sign up to request clarification or add additional context in comments.

1 Comment

But how can i parse the image? Like from the request?
0

This package solved my problems: https://www.npmjs.com/package/formidable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.