Node JS错误:ENOENT

89

我正在跟随:Node初学者入门

在尝试另一篇stackoverflow帖子中的代码后:

var Fs = require('fs');

var dirs = ['tmp'];
var index;
var stats;

for (index = 0; index < dirs.length; ++index)
{
    try
    {
        stats = Fs.lstatSync(dirs[index]);
        console.log(dirs[index] + ": is a directory? " + stats.isDirectory());
    }
    catch (e)
    {
        console.log(dirs[index] + ": " + e);
    }
}

错误仍然存在:

错误:ENOENT,'tmp'文件或目录不存在

应用程序目录结构

tmp文件夹的权限为777。

requestHandlers.js

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

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

  var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" '+
    'content="text/html; charset=UTF-8" />'+
    '<style>input{display: block; margin: 1em 0;}</style>'+
    '</head>'+
    '<body>'+
    '<form action="/upload" method="post">'+
    '<textarea name="text" rows="20" cols="60"></textarea>'+
    '<input type="submit" value="Submit text" />'+
    '</form>'+
    '</body>'+
    '</html>';

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

function upload(response, postData) {
  console.log("Request handler 'upload' was called.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("You've sent the text: "+
  querystring.parse(postData).text);
  response.end();
}

function show(response, postData) {
  console.log("Request handler 'show' was called.");
  fs.readFile("/tmp/test.jpg", "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/jpg"});
      response.write(file, "binary");
      response.end();
    }
  });
}

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

Index.js

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;

server.start(router.route, handle);

有点困惑,感谢任何帮助。


1
你的 process.cwd() 是什么? - fent
3
在这里找到了解决方案:https://dev59.com/5FvUa4cB1Zd3GeqPy_1D。喜欢这个论坛! - Wasabi
6个回答

91

"/tmp/test.jpg" 不是正确的路径 - 这个路径以 / 开头,表示根目录。

在Unix中,当前目录的快捷方式为 .

尝试使用这个路径 "./tmp/test.jpg"


5
他的解释完全正确。/tmp 不同于 ./tmp/tmp 不在当前目录中,它在根目录中。 - 3ocene

18
稍微解释一下错误原因:在路径开头加上一个正斜线意味着“从文件系统的根目录开始,并查找给定的路径”。没有正斜线则表示“从当前工作目录开始,并查找给定的路径”。
路径:
/tmp/test.jpg
因此,将文件test.jpg在文件系统根目录下的tmp文件夹中查找(例如,在Windows上为c:\,在*nix上为/),而不是在webapp文件夹中查找。在路径前添加一个句点(.)明确地将其更改为“从当前工作目录开始”,但基本上与完全省略正斜杠相同。
./tmp/test.jpg = tmp/test.jpg

8
如果您的tmp文件夹相对于代码运行的目录,请移除/tmp前面的/。因此,您的代码中只需使用tmp/test.jpg即可。在类似的情况下,这对我很有效。

1

我能提供的另一个帮助是:

const path = require('path');
const filePath = path.join(__dirname, './path/filename.ext');

我之前只使用了'./paths',遇到了ENOENT的问题,但上述方法解决了这个问题。


1

改变

"/tmp/test.jpg".

"./tmp/test.jpg"

0

您可以将不同目录中的Jade文件包含到您的模板中

views/
     layout.jade
static/
     page.jade

将视图目录中的布局文件包含到 static/page.jade 文件中。

page.jade

extends ../views/layout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接