var request = require('request');
var fs = require('fs');
var url = require('url');

var fetchURL = function(url, path) {
  request(url).pipe(fs.createWriteStream('/Users/Kristina/js_books/temp/images'));
};

module.exports = function(app) {
  //home route
  var home = require('../app/controllers/home');
  app.get('/', home.index);
  
  //image route  
  var imageDataController = require('../app/controllers/ImageData.js');
  app.post('/image', function(req, response) {
    

    //store metadata to db 
    var result = imageDataController.storeImageMetaData(req, response);
    
    //load file data to server
    fetchURL(req.body.imgUrl);

    //send response
    response.writeHead(200, {'Content-Type': 'application/json'});
    response.end(JSON.stringify(result));
  });
};


@@ -77,16 +77,146 @@
 'url');%0A
+var im = require('imagemagick');%0Avar imageDataController = require('../app/controllers/ImageData.js');%0A%0A//load file data to server
 %0Avar fet
@@ -245,94 +245,484 @@
 path
+, cb
 ) %7B%0A  
-request(url).pipe(fs.createWriteStream('/Users/Kristina/js_books/temp/images'));
+var picStream = fs.createWriteStream(path);%0A  picStream.on('close', function() %7B%0A    cb();%0A  %7D);%0A  request(url).pipe(picStream);%0A%0A%7D;%0A%0A//store metadata to db %0Avar addMetaData = function(path, cb) %7B%0A%0A  //add s3 url%0A  im.identify('/Users/Kristina/js_books/temp/images', function(error, features) %7B%0A    if (error) %7B%0A      throw new Error('%3C/3', error);%0A    %7D%0A%0A    console.log('features', features);%0A    //imageDataController.storeImageMetaData(features);%0A  %7D);%0A%0A  cb();%0A
 %0A%7D;%0A
@@ -872,80 +872,8 @@
 e  %0A
-  var imageDataController = require('../app/controllers/ImageData.js');%0A
   ap
@@ -918,21 +918,16 @@
 e) %7B
-%0A    
 %0A%0A    //
 stor
@@ -926,189 +926,188 @@
   //
-store metadata to db %0A    var result = imageDataController.storeImageMetaData(req, response);%0A    %0A    //load file data to server%0A    fetchURL(req.body.imgUrl);%0A%0A    //send 
+add env%5Bvar%5D%0A    path = '/Users/Kristina/js_books/temp/images';%0A%0A    fetchURL(req.body.imgUrl, path, function() %7B%0A      addMetaData(path, function() %7B%0A        console.log('
 response
 %0A   
@@ -1102,17 +1102,24 @@
 response
+');
 %0A
+    
     resp
@@ -1140,49 +1140,15 @@
 (200
-, %7B'Content-Type': 'application/json'%7D
 );%0A
+    
     
@@ -1164,30 +1164,26 @@
 end(
-JSON.stringify(result)
+);%0A      %7D);%0A    %7D
 );%0A 
@@ -1189,10 +1189,8 @@
   %7D);%0A%7D;
-%0A%0A

add listener for close event when streaming, to store meta data in mongo