Node.js 为什么我可以使用绝对路径而不是相对路径将文件上载到文件夹?

Node.js 为什么我可以使用绝对路径而不是相对路径将文件上载到文件夹?,node.js,file-upload,relative-path,absolute-path,formidable,Node.js,File Upload,Relative Path,Absolute Path,Formidable,和其他很多新手一样,我也在关注曼努埃尔·基斯林的《节点初学者书》 我们应该通过强大的模块实现图像上传,不断地将图像重命名为“test.png”并显示它 这本书使用路径/tmp/test.png,一切正常。但这不是一个相对路径,我的图像实际上保存在Linux中的tmp文件夹中 我在我的项目中创建了文件夹“tmp”,并试图在那里实现同样的事情,但我只看到一个错误 ENOENT, open './tmp/test.png' 当我尝试路径tmp/test.png时也会发生同样的情况,但有趣的是,当我将

和其他很多新手一样,我也在关注曼努埃尔·基斯林的《节点初学者书》

我们应该通过强大的模块实现图像上传,不断地将图像重命名为“test.png”并显示它

这本书使用路径
/tmp/test.png
,一切正常。但这不是一个相对路径,我的图像实际上保存在Linux中的tmp文件夹中

我在我的项目中创建了文件夹“tmp”,并试图在那里实现同样的事情,但我只看到一个错误

ENOENT, open './tmp/test.png'
当我尝试路径
tmp/test.png
时也会发生同样的情况,但有趣的是,当我将实际图像直接放入tmp文件夹并在页面上显示它时,这两个路径都起作用。所以语义/语法没有问题,或者

My requestHandler.js,其中重要的部分是上载和显示功能:

var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");

function start(response) {
  console.log("Request handler 'start' was called.");

    var body = '<html>' +
        '<head>' +
        '<meta http-equiv="Content-Type" content="text/html; ' +
        'charset=UTF-8" />' +
        '</head>' +
        '<body>' +
        '<form action="/upload" enctype="multipart/form-data" ' +
        'method="post">' +
        '<input type="file" name="upload" multiple="multiple">' +
        '<input type="submit" value="Upload file" />' +
        '</form>' +
        '</body>' +
        '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();

}

function upload(response, request) {
  console.log("Request handler 'upload' was called.");

  var form = new formidable.IncomingForm();
  console.log("about to parse");
  form.parse(request, function(error, fields, files) {
    console.log("parsing done");

    fs.rename(files.upload.path, "./tmp/test.png", function(err) {
        if (err) {
            console.log("Error Flynn");
            fs.unlink("./tmp/test.png");
            fs.rename(files.upload.path, "./tmp/test.png");
        }
    });

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("received image:<br/>");
    response.write("<img src='/show' />");
    response.end();
});
}

function show(response) {
  console.log("Request handler 'show' was called.");
  fs.readFile("./tmp/test.png", "binary", function(error, file) {
    if(error) {
        response.writeHead(500, {"Content-Type": "text/plain"});
        response.write(error + "\n");
        response.end();
    }
    else {
        response.writeHead(200, {"Content-Type": "image/png"});
        response.write(file, "binary");
        response.end();
    }
  });
}

exports.start = start;
exports.upload = upload;
exports.show = show;

相对路径起作用,但它们是相对于
process.cwd()
,而不是当前执行的模块的


\uuu dirname
正在引用当前的
.js
文件

从未听说过
process.cwd()
,但我会查找它。我应该使用什么语法?另一个未回答的问题:它如何找到文件以使用相同的精确路径删除它,但不重命名它?我的意思是相对路径是相对于节点进程的目录的。因此,您启动节点应用程序的目录。
\uuu dirname
是您可以使用的语法,因为它指的是使用它的文件的de dir。这是一种相当常见的方法…使用
fs.unlink()
删除文件。对不起,伙计,你没有阅读我的问题,而且肯定没有回答我的问题。:)我知道unlink的作用。我不知道当路径不起作用时它怎么能做。当然,我已经创建了相对于节点进程路径的文件夹。所以所有的事情都应该是可行的,但事实并非如此。我真的想澄清一下。
Request for /upload received.
About to route a request for /upload
Request handler 'upload' was called.
about to parse
parsing done
Error Flynn
fs: missing callback Error: ENOENT, unlink './tmp/test.png'
fs: missing callback Error: EXDEV, rename '/tmp/upload_9bcb8afa8cd2f78ff52c294edd106965'
Request for /show received.
About to route a request for /show
Request handler 'show' was called.