最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js EXDEV rename error - Stack Overflow

programmeradmin1浏览0评论

I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.

It shows the EXDEV error (500 Error: EXDEV, rename).

Could someone give me a hint? Here's my code:

exports.submit = function(dir) {
    return function(req, res, next) {
        var img = req.files.photo.image;
        var name = req.body.photo.name || img.name;
        var path = join(dir, img.name);

        fs.rename(img.path, path, function (err) {
            if(err) return next(err);

            Photo.create({
                name: name,
                path: img.name
            }, function (err) {
                if(err) return next(err);
                res.redirect('/');
            });
        });
    };
};

I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.

It shows the EXDEV error (500 Error: EXDEV, rename).

Could someone give me a hint? Here's my code:

exports.submit = function(dir) {
    return function(req, res, next) {
        var img = req.files.photo.image;
        var name = req.body.photo.name || img.name;
        var path = join(dir, img.name);

        fs.rename(img.path, path, function (err) {
            if(err) return next(err);

            Photo.create({
                name: name,
                path: img.name
            }, function (err) {
                if(err) return next(err);
                res.redirect('/');
            });
        });
    };
};
Share Improve this question edited Dec 4, 2014 at 11:56 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jan 12, 2014 at 4:25 user2837851user2837851 1,1913 gold badges15 silver badges20 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 21

Renaming files cannot be done cross-device. My guess is that your upload directory (which by default is /tmp) is on another partition/drive as your target directory (contained in the dir variable).

Some solutions:

  • configure the upload directory to be on the same partition/drive as your target directory; this depends on which module you're using to handle file uploads, express.bodyParser (and the module it uses, connect.multipart) accepts an uploadDir option that you can use;
  • before starting your Node app, set the TMPDIR environment variable to point to a temporary directory on the same partition/drive as your target directory. If you're using a Unix-type OS:

    env TMPDIR=/path/to/directory node app.js
    
  • instead of setting the environment variable from your shell, set it at the top of your Node app:

    process.env.TMPDIR = '/path/to/directory';
    
  • instead of renaming, use a module like mv that can work cross-device;

Using Windows XP, I added to app.js:

process.env.TMPDIR = '.';  //new
发布评论

评论列表(0)

  1. 暂无评论