Express.js抛出错误:Linux服务器上的ENOENT

时间:2013-01-15 01:46:55

标签: linux node.js express forever

所以,我一直在处理我在Windows机器上工作的上传脚本(下面的代码),但是在我的Linux服务器上它似乎失败了

Error: ENOENT, open 'uploads/da5ab67b48ea2deecd25127186017083'

我理解错误是说没有路径/文件,但我在尝试重命名文件之前检查文件是否存在。

exports.product_files_upload = function (req, res) {
    if (req.files) {
        var tmpArray = [];
        for(var file in req.files){
            console.log(file)
            if (file){
                var splitName = file.split('-');
                if (splitName.length > 1) {
                    var username = splitName[0];
                    var productId = splitName[1];
                    var index = splitName[2];
                } else {
                    return;
                }
            } else {
                return;
            }
            //console.log(username)
            //console.log(productId)
            var tmp_path = req.files[file].path;
            console.log(fs.existsSync(tmp_path))
            createUserDir();
            createProductDirs(username, productId);
            //console.log(__dirname)
            var target_path = './public/user_files/'+ username + '/products/'+productId+'/files/' + req.files[file].name,
                save_path = 'user_files/'+ username + '/products/'+productId+'/files/' + req.files[file].name;
            if (fs.existsSync(target_path)) {
                tmpArray.push({exists:true, name:req.files[file].name});
            } else {
                tmpArray.push({
                    size: req.files[file].size,
                    type: req.files[file].type,
                    name: req.files[file].name,
                    path: save_path,
                    exists:false
                });
                if (fs.existsSync(tmp_path)) {
                    fs.rename(tmp_path, target_path, function(err) {
                        if (err) throw err;
                        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
                        fs.unlink(tmp_path, function() {
                            if (err) throw err;
        //                    res.send(save_path);

                        });
                    });
                } else {
                    tmpArray.push({
                        size: req.files[file].size,
                        type: req.files[file].type,
                        name: req.files[file].name,
                        path: save_path,
                        exists:false
                    });
                }
            }
        }
        res.json(tmpArray);
    }

};

更新:我正在使用永远运行我的快递应用程序,当我停止我的应用程序永远处理并切换到“节点app.js”问题得到解决。这不是一个可接受的修复方法。我想可能永远存在问题。

1 个答案:

答案 0 :(得分:1)

好的,我明白了。我一直在使用绝对路径在我的linux盒子上启动我的应用程序,如

forever start /path/to/app.js

但是,当我进入app目录然后启动应用程序时,它可以工作。

forever start app.js