2016-04-27 9 views
1

ユーザーが画像をアップロードした後にSharpを使用して画像のサイズを変更するという明示的なアプリがあります。 'npm start'を使用してアプリケーションを起動すると、問題なくイメージをアップロードできますが、PM2を使用してプロセスを管理すると、イメージはサーバーに保存されません。私はSharpを使用せずにそれらのサイズを変更することができます。私が救い出されないという鋭いコードを持っているときだけです。以下は私のコントローラのコードです。 Multerはフォームを処理しており、シャープは画像のサイズを変更しています。PM2でアプリを実行しているときに鮮明な画像アップローダーが動作しない

doNewRecipe: function(req, res) { 
    for (var key in req.body) { 
     req.body[key] = req.body[key] || undefined; 
    } 
    var body = _.pick(req.body, 'title', 'description', 'ingredients', 'instructions', 'yield', 'prep_time', 'cook_time', 'categoryId'); 
    body.userId = req.session.user.id; 

    if (req.file) { 
     var tempPath = req.file.path, 
      ext = path.extname(req.file.originalname).toLowerCase(), 
      //targetPath = path.resolve(finalUploadPath + req.file.filename + ext); 
      targetPath = path.resolve(finalUploadPath); 
     fs.renameSync(tempPath, tempPath + ext); 
     var newFileName = req.file.filename + ext; 
     var imageFile = tempPath + ext; 


     body.image = newFileName; 

     sharp(imageFile) 
      .resize(450, 450) 
      .max() 
      .toFile('./public/finalUpload/' + newFileName, function(err, info) { 
       body.image = newFileName; 
       fs.unlinkSync(path.resolve(tempPath + ext)); 

       db.recipe.create(body).then(function(recipe) { 
        res.redirect('/recipe/view/' + recipe.id); 
       }, function(e) { 
        console.log(e.message); 
        res.render('error', {message: e.toString()}); 
       }); 
      }); 


     //fs.renameSync(tempPath, targetPath); 
    } else { 

     db.recipe.create(body).then(function(recipe) { 
      res.redirect('/recipe/view/' + recipe.id); 
     }, function(e) { 
      console.log(e.message); 
      res.render('error', {message: e.toString()}); 
     }); 
    } 
}, 
+0

PM2でアプリを起動するには、ecosystem.json設定ファイルを使用していますか? CWDパラメータを追加できます。 – Zauker

+0

私は 'pm2 start bin/www'を使ってアプリを起動しています。 –

+0

これは同じではありません。 pm2 start /my/path/app.js 次に cd/my/path; pm2 start app.js あなたのアプリの起動オプションでjsonファイルを生成し、 'cwd'(作業ディレクトリのパス)を追加することもできます。 – Zauker

答えて

0

ファイルを保存する場所をpathで指定すると、システム間で簡単に転送されます。私は実際にファイルを保存する場所を決定するためにいくつかの変数を作成しました。以下は私のコードです。

if (process.env.NODE_ENV === 'production') { 
    var finalUploadPath = 'hidden'; 
    var googleTracking = true; 
} else if (process.env.NODE_ENV === 'staging') { 
    var finalUploadPath = 'hidden'; 
    var googleTracking = false; 
} else { 
    var finalUploadPath = 'hidden'; 
    var googleTracking = false; 
} 

sharp(imageFile) 
      .resize(450, 450) 
      .max() 
      .toFile(finalUploadPath + req.file.filename + '.jpg', function(err, info) { 
       body.image = req.file.filename + '.jpg'; 
       fs.unlinkSync(path.resolve(tempPath + ext)); 

       compressAndResize(path.resolve(__dirname, '../public/finalUpload/' + body.image)); 

       db.recipe.create(body).then(function(recipe) { 
        req.flash('success', 'Recipe created'); 
        res.redirect('/recipe/view/' + recipe.id + '/' + recipe.slug); 
       }, function(e) { 
        console.log(e.message); 
        req.flash('error', 'Error creating new recipe'); 
        res.render('error', {message: e.toString(), csrfToken: req.csrfToken(), google: googleTracking}); 
       }); 
      }); 
関連する問題