Express 使用PM2运行应用程序时,Sharp image uploader不工作

Express 使用PM2运行应用程序时,Sharp image uploader不工作,express,npm,multer,Express,Npm,Multer,我有一个express应用程序,在用户上传图像后,我正在使用Sharp调整图像大小。当我使用“npm start”启动应用程序时,我能够上传没有问题的图像,但是如果我使用PM2管理该过程,图像不会保存在服务器上。我可以保存它们而不用使用夏普来调整它们的大小,只有当我有夏普的代码时,它们才不会被保存。下面是我的控制器的代码。Multer正在处理表单,sharp正在调整图像的大小 doNewRecipe: function(req, res) { for (var key in req.bo

我有一个express应用程序,在用户上传图像后,我正在使用Sharp调整图像大小。当我使用“npm start”启动应用程序时,我能够上传没有问题的图像,但是如果我使用PM2管理该过程,图像不会保存在服务器上。我可以保存它们而不用使用夏普来调整它们的大小,只有当我有夏普的代码时,它们才不会被保存。下面是我的控制器的代码。Multer正在处理表单,sharp正在调整图像的大小

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()});
        });
    }
},

如果使用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});
                });
            });

如果使用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});
                });
            });

您是否使用ecosystem.json配置文件启动PM2应用程序?你可以添加CWD参数。我刚刚使用'pm2 start bin/www'启动应用程序。执行pm2 start/my/path/app.js和执行cd/my/path不一样;pm2 start app.js您可以使用应用程序的start选项生成一个json文件,还可以添加“cwd”(工作目录的路径)@AndyBecker我也有同样的问题,解决了吗?您是否使用ecosystem.json配置文件使用pm2启动应用程序?你可以添加CWD参数。我刚刚使用'pm2 start bin/www'启动应用程序。执行pm2 start/my/path/app.js和执行cd/my/path不一样;pm2 start app.js您可以使用应用程序的start选项生成一个json文件,并添加“cwd”(工作目录的路径)@AndyBecker我也有同样的问题,解决这个问题的运气好吗?