node.js 节点 JS 错误:ENOENT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9047094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node JS Error: ENOENT
提问by Wasabi
I'm following along with: The Node Beginner Book
我正在关注: The Node Beginner Book
After testing with the code from another SO post:
使用另一篇 SO 帖子中的代码进行测试后:
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);
}
}
The error persist:
错误仍然存在:
Error: ENOENT, no such file or directory 'tmp'
错误:ENOENT,没有这样的文件或目录“tmp”


The permissions on tmp are 777.
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
索引.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);
A little stumped, any help appreciated.
有点难住,任何帮助表示赞赏。
回答by ihciad
"/tmp/test.jpg"is not the correct path – this path starts with /which is the root directory.
"/tmp/test.jpg"不是正确的路径 - 此路径以/根目录开头。
In unix, the shortcut to the current directory is .
在 unix 中,当前目录的快捷方式是 .
Try this "./tmp/test.jpg"
尝试这个 "./tmp/test.jpg"
回答by Jens Wegar
To expand a bit on why the error happened: A forward slash at the beginning of a path means "start from the root of the filesystem, and look for the given path". No forward slash means "start from the current working directory, and look for the given path".
详细说明错误发生的原因:路径开头的正斜杠表示“从文件系统的根目录开始,并查找给定的路径”。没有正斜杠意味着“从当前工作目录开始,并寻找给定的路径”。
The path
路径
/tmp/test.jpg
thus translates to looking for the file test.jpgin the tmp folder at the root of the filesystem (e.g. c:\ on windows, / on *nix), instead of the webappfolder. Adding a period (.) in front of the path explicitly changes this to read "start from the current working directory", but is basically the same as leaving the forward slash out completely.
因此转换为在文件系统根目录的 tmp 文件夹中查找文件test.jpg(例如 c:\ 在 windows 上,/ 在 *nix 上),而不是webapp文件夹。在路径前添加句点 (.) 将其显式更改为“从当前工作目录开始”,但与完全保留正斜杠基本相同。
./tmp/test.jpg = tmp/test.jpg
回答by Dave Howell
if your tmp folder is relative to the directory where your code is running remove the /in front of /tmp.
如果您的TMP文件夹是相对于您的代码运行除去目录/面前/tmp。
So you just have tmp/test.jpgin your code. This worked for me in a similar situation.
所以你只是tmp/test.jpg在你的代码中。在类似的情况下,这对我有用。
回答by Adeojo Emmanuel IMM
change
改变
"/tmp/test.jpg".
to
到
"./tmp/test.jpg"
回答by Bastin Robin
You can include a different jade file into your template, that to from a different directory
您可以在模板中包含不同的 jade 文件,该文件来自不同的目录
views/
layout.jade
static/
page.jade
To include the layout file from views dir to static/page.jade
将布局文件从视图目录包含到 static/page.jade
page.jade
页面.jade
extends ../views/layout
回答by sQuijeW
use "temp" in lieu of "tmp"
使用“temp”代替“tmp”
"/temp/test.png"
“/temp/test.png”
it worked for me after i realized the tmp is a temporary folder that didn't exist on my computer, but my temp was my temporary folder
在我意识到 tmp 是我的计算机上不存在的临时文件夹后,它对我有用,但我的临时文件夹是我的临时文件夹
///
///
EDIT:
编辑:
I also created a new folder "tmp" in my C: drive and everything worked perfectly. The book may have missed mentioning that small step
我还在我的 C: 驱动器中创建了一个新文件夹“tmp”,一切正常。这本书可能没有提到那一小步
check out http://webchat.freenode.net/?channels=node.jsto chat with some of the node.js community
查看http://webchat.freenode.net/?channels=node.js与一些 node.js 社区聊天

